How can I check for available disk space?

可紊 提交于 2019-11-30 04:43:45

You can check it by doing the following:

Add the System.Management.dll as a reference to your project.

Use the following code to get the diskspace:

using System;
using System.Management;

public string GetFreeSpace();
{ 
   ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
   disk.Get();
   string freespace = disk["FreeSpace"];
   return freespace;
}

There are a myriad of ways to do it, I'd check the System.Management namespace for more ways.

Here's one such way from that page:

public void GetDiskspace()
    {
      ConnectionOptions options = new ConnectionOptions();
      ManagementScope scope = new ManagementScope("\\\\localhost\\root\\cimv2", 
      options);
      scope.Connect();
      ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
      SelectQuery query1 = new SelectQuery("Select * from Win32_LogicalDisk");

      ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
      ManagementObjectCollection queryCollection = searcher.Get();
      ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(scope, query1);
      ManagementObjectCollection queryCollection1 = searcher1.Get();

      foreach (ManagementObject m in queryCollection)
      {
          // Display the remote computer information

          Console.WriteLine("Computer Name : {0}", m["csname"]);
          Console.WriteLine("Windows Directory : {0}", m["WindowsDirectory"]);
          Console.WriteLine("Operating System: {0}", m["Caption"]);
          Console.WriteLine("Version: {0}", m["Version"]);
          Console.WriteLine("Manufacturer : {0}", m["Manufacturer"]);
          Console.WriteLine();
      }

      foreach (ManagementObject mo in queryCollection1)
      {
          // Display Logical Disks information

         Console.WriteLine("              Disk Name : {0}", mo["Name"]);
         Console.WriteLine("              Disk Size : {0}", mo["Size"]);
         Console.WriteLine("              FreeSpace : {0}", mo["FreeSpace"]);
         Console.WriteLine("          Disk DeviceID : {0}", mo["DeviceID"]);
         Console.WriteLine("        Disk VolumeName : {0}", mo["VolumeName"]);
         Console.WriteLine("        Disk SystemName : {0}", mo["SystemName"]);
         Console.WriteLine("Disk VolumeSerialNumber : {0}", mo["VolumeSerialNumber"]);
         Console.WriteLine();
      }
      string line;
      line = Console.ReadLine(); 
    }

by using this code

 static void Main()
    {
        try
        {
            DriveInfo driveInfo = new DriveInfo(@"C:");
            long FreeSpace = driveInfo.AvailableFreeSpace;
        }
        catch (System.IO.IOException errorMesage)
        {
            Console.WriteLine(errorMesage);
        }

    }

IF you are getting the error 'The device is not ready' .i.e your device is not ready . If you are trying this code for a CD drive without CD you will get the same error : )

This seems to be an option from the System.IO:

DriveInfo c = new DriveInfo("C");
long cAvailableSpace = c.AvailableFreeSpace;
Jason Irwin

You can use the DriveInfo class

DriveInfo[] oDrvs = DriveInfo.GetDrives();
    foreach (var Drv in oDrvs) {
        if (Drv.IsReady) {
            Console.WriteLine(Drv.Name + " " + Drv.AvailableFreeSpace.ToString);
        }
}
Cleiton

You can use WMI, see this related question:

Best way to query disk space on remote server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!