Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

c# - How can I check for available disk space?

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...