【问题标题】:How do I retrieve disk information in C#?如何在 C# 中检索磁盘信息?
【发布时间】:2010-09-29 14:00:31
【问题描述】:

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

【问题讨论】:

    标签: c# disk


    【解决方案1】:

    对于大多数信息,您可以使用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】:

    如果您想在本地计算机上获取单个/特定驱动器的信息。您可以使用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】:

      没有驱动器号的已安装卷怎么办?

      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"));
        }
      }
      

      【讨论】:

      • 我自己发现的:''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")); } } }
      【解决方案4】:

      【讨论】:

        【解决方案5】:

        检查DriveInfo 类,看看它是否包含您需要的所有信息。

        【讨论】:

          【解决方案6】:

          在 ASP .NET Core 3.1 中,如果您想获得在 windows 和 linux 上都可以运行的代码,您可以按如下方式获取驱动器:

          var drives = DriveInfo
              .GetDrives()
              .Where(d => d.DriveType == DriveType.Fixed)
              .Where(d => d.IsReady)
              .ToArray();
          

          如果你不同时应用这两个 where,如果你在 linux 中运行代码,你会得到很多驱动器(例如“/dev”、“/sys”、“/etc/hosts”等)。

          这在开发应用程序以在 Linux Docker 容器中运行时特别有用。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-12-03
            • 2016-08-26
            • 2020-03-01
            • 2019-08-06
            • 2011-12-08
            • 1970-01-01
            • 2014-02-14
            相关资源
            最近更新 更多