当前位置:  开发笔记 > 编程语言 > 正文

如何在C#中检索磁盘信息?

如何解决《如何在C#中检索磁盘信息?》经验,为你挑选了4个好方法。

我想使用C#访问计算机上逻辑驱动器的信息.我该怎么做到这一点?谢谢!



1> Vinko Vrsalo..:

对于大多数信息,您可以使用DriveInfo类.

using System;
using System.IO;

class Info {
    public static void Main() {
        DriveInfo[] drives = DriveInfo.GetDrives();
        foreach (DriveInfo drive in drives) {
            //There are more attributes you can use.
            //Check the MSDN link for a complete example.
            Console.WriteLine(drive.Name);
            if (drive.IsReady) Console.WriteLine(drive.TotalSize);
        }
    }
}



2> mmushtaq..:

如果要在本地计算机上获取有关单个/特定驱动器的信息。您可以使用DriveInfo类按照以下步骤进行操作:

//C Drive Path, this is useful when you are about to find a Drive root from a Location Path.
string path = "C:\\Windows";

//Find its root directory i.e "C:\\"
string rootDir = Directory.GetDirectoryRoot(path);

//Get all information of Drive i.e C
DriveInfo driveInfo = new DriveInfo(rootDir); //you can pass Drive path here e.g   DriveInfo("C:\\")

long availableFreeSpace = driveInfo.AvailableFreeSpace;
string driveFormat = driveInfo.DriveFormat;
string name = driveInfo.Name;
long totalSize = driveInfo.TotalSize;



3> 小智..:

使用System.IO.DriveInfo类 http://msdn.microsoft.com/zh-cn/library/system.io.driveinfo.aspx



4> Foozinator..:

如果您没有驱动器号,那么装入的卷怎么样?

foreach( ManagementObject volume in 
             new ManagementObjectSearcher("Select * from Win32_Volume" ).Get())
{
  if( volume["FreeSpace"] != null )
  {
    Console.WriteLine("{0} = {1} out of {2}",
                  volume["Name"],
                  ulong.Parse(volume["FreeSpace"].ToString()).ToString("#,##0"),
                  ulong.Parse(volume["Capacity"].ToString()).ToString("#,##0"));
  }
}

推荐阅读
围脖上的博博_771
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有