【问题标题】:Getting a list of logical drives获取逻辑驱动器列表
【发布时间】:2010-10-21 09:08:43
【问题描述】:

如何获取系统上的逻辑驱动器 (C#) 列表以及它们的容量和可用空间?

【问题讨论】:

    标签: c# hard-drive


    【解决方案1】:

    【讨论】:

    • 这是最新版本的 .NET 中添加的新内容。我在几年前写了一个小应用程序来展示,但当时不得不走 WMI 路线。无论如何都很方便知道......干杯
    • 快速浏览 MSDN:在 .NET 2.0 中添加。
    【解决方案2】:
    foreach (var drive in DriveInfo.GetDrives())
    {
        double freeSpace = drive.TotalFreeSpace;
        double totalSpace = drive.TotalSize;
        double percentFree = (freeSpace / totalSpace) * 100;
        float num = (float)percentFree;
    
        Console.WriteLine("Drive:{0} With {1} % free", drive.Name, num);
        Console.WriteLine("Space Remaining:{0}", drive.AvailableFreeSpace);
        Console.WriteLine("Percent Free Space:{0}", percentFree);
        Console.WriteLine("Space used:{0}", drive.TotalSize);
        Console.WriteLine("Type: {0}", drive.DriveType);
    }
    

    【讨论】:

      【解决方案3】:

      Directory.GetLogicalDrives

      他们的例子更健壮,但这就是它的症结

      string[] drives = System.IO.Directory.GetLogicalDrives();
      
      foreach (string str in drives) 
      {
          System.Console.WriteLine(str);
      }
      

      您也可以P/Invoke 并调用 win32 函数(或者如果您在非托管代码中使用它)。

      这只会得到一个驱动器列表,对于每个驱动器的信息,您需要使用GetDrives,正如 Chris Ballance 所演示的那样。

      【讨论】:

        【解决方案4】:

        也许这就是你想要的:

        listBox1.Items.Clear();
        
        foreach (DriveInfo f in DriveInfo.GetDrives())    
            listBox1.Items.Add(f);
        

        【讨论】:

        • 您可能还想检查 IsReady 属性
        【解决方案5】:

        您可以使用 Windows Management Instrumentation (WMI) 检索此信息

         using System.Management;
        
            ManagementObjectSearcher mosDisks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
            // Loop through each object (disk) retrieved by WMI
            foreach (ManagementObject moDisk in mosDisks.Get())
            {
                // Add the HDD to the list (use the Model field as the item's caption)
                Console.WriteLine(moDisk["Model"].ToString());
            }
        

        这里有更多关于你可以投票的属性的信息

        http://www.geekpedia.com/tutorial233_Getting-Disk-Drive-Information-using-WMI-and-Csharp.html

        【讨论】:

        • 无法在我的电脑上运行。 System.Management 现在没有 ManagementObjectSearcher 类。该 URL 也未指向有效的网页。
        • 您需要为此添加参考。在 Visual Studio 上,右键单击项目,然后转到添加 -> 参考。然后,搜索“System.Management”并添加它。
        【解决方案6】:

        这是一段很棒的代码。

        ObjectQuery query =
            new ObjectQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType=3"); // Create query to select all the hdd's
        
        ManagementObjectSearcher searcher =
          new ManagementObjectSearcher(scope, query); // run the query
        
        ManagementObjectCollection queryCollection = searcher.Get(); // get the results
        string sVolumeLabel = "";
        string[,] saReturn = new string[queryCollection.Count, 7];
        int i = 0; // counter for foreach
        
        foreach (ManagementObject m in queryCollection)
        {
          if (string.IsNullOrEmpty(Convert.ToString(m["VolumeName"]))) { sVolumeLabel = "Local Disk"; } else { sVolumeLabel = Convert.ToString(m["VolumeName"]); } // Disk Label
          string sSystemName = Convert.ToString(m["SystemName"]); // Name of computer
          string sDriveLetter = Convert.ToString(m["Name"]); // Drive Letter
        
          decimal dSize = Math.Round((Convert.ToDecimal(m["Size"]) / 1073741824), 2); //HDD Size in Gb
          decimal dFree = Math.Round((Convert.ToDecimal(m["FreeSpace"]) / 1073741824), 2); // Free Space in Gb
          decimal dUsed = dSize - dFree; // Used HDD Space in Gb
        
          int iPercent = Convert.ToInt32((dFree / dSize) * 100); // Percentage of free space
        
          saReturn[i,0] = sSystemName;
          saReturn[i,1] = sDriveLetter;
          saReturn[i,2] = sVolumeLabel;
          saReturn[i,3] = Convert.ToString(dSize);
          saReturn[i,4] = Convert.ToString(dUsed);
          saReturn[i,5] = Convert.ToString(dFree);
          saReturn[i,6] = Convert.ToString(iPercent);
        
          i++; // increase counter. This will add the above details for the next drive.
        }
        

        【讨论】:

          【解决方案7】:

          我部署了一个 SSIS 包并收到错误只是因为在 Ballance 示例中我们正在读取驱动器而不等待它们准备好:

          string EmailMessage = "";
          foreach (var drive in System.IO.DriveInfo.GetDrives())
          {
              if (drive.IsReady == true) /*Make sure we can read*/
              {
                  double freeSpace = drive.TotalFreeSpace;
                  double totalSpace = drive.TotalSize;
                  double percentFree = (freeSpace / totalSpace) * 100;
                  float num = (float)percentFree;
                  //MessageBox.Show("Drive:" + drive.Name + " With " + num + "% free.");
                  if (num < 5)
                  {
                      EmailMessage = "Drive:" + drive.Name + " With " + num + "% free.";
                  }
              }
              
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-02-13
            • 1970-01-01
            • 2011-09-14
            • 1970-01-01
            • 2021-04-21
            • 2017-04-13
            • 2020-12-15
            相关资源
            最近更新 更多