如何检查可用磁盘空间?

时间:2022-04-22 16:51:43

I need a way to check available disk space on a remote Windows server before copying files to that server. Using this method I can check to see if the primary server is full and if it is, then I'll copy the files to a secondary server.

在将文件复制到该服务器之前,我需要一种方法来检查远程Windows服务器上的可用磁盘空间。使用此方法,我可以检查主服务器是否已满,如果是,则我将文件复制到辅助服务器。

How can I check for available disk space using C#/ASP.net 2.0?

如何使用C#/ ASP.net 2.0检查可用磁盘空间?

5 个解决方案

#1


30  

You can check it by doing the following:

您可以通过执行以下操作来检查它:

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

添加System.Management.dll作为项目的引用。

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.

有很多方法可以做到这一点,我会检查System.Management命名空间以获取更多方法。

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

#2


16  

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 : )

如果您收到错误“设备尚未就绪”。即您的设备尚未就绪。如果您正在为没有CD的CD驱动器尝试此代码,您将收到相同的错误:)

#3


8  

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

这似乎是System.IO的一个选项:

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

#4


5  

You can use the DriveInfo class

您可以使用DriveInfo类

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

#5


1  

You can use WMI, see this related question:

您可以使用WMI,请参阅此相关问题:

Best way to query disk space on remote server

查询远程服务器上磁盘空间的最佳方法

#1


30  

You can check it by doing the following:

您可以通过执行以下操作来检查它:

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

添加System.Management.dll作为项目的引用。

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.

有很多方法可以做到这一点,我会检查System.Management命名空间以获取更多方法。

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

#2


16  

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 : )

如果您收到错误“设备尚未就绪”。即您的设备尚未就绪。如果您正在为没有CD的CD驱动器尝试此代码,您将收到相同的错误:)

#3


8  

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

这似乎是System.IO的一个选项:

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

#4


5  

You can use the DriveInfo class

您可以使用DriveInfo类

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

#5


1  

You can use WMI, see this related question:

您可以使用WMI,请参阅此相关问题:

Best way to query disk space on remote server

查询远程服务器上磁盘空间的最佳方法