views:

1596

answers:

5

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?

A: 

You can use WMI, see this related question:

http://stackoverflow.com/questions/56715/best-way-to-query-disk-space-on-remote-server

Cleiton
+3  A: 

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(); 
    }
George Stocker
This works for the machine that is executing the code, but what about for a remote server?
Bryan Denny
The link I sent you to can tell you how; I've not done it; but I can post the code you'd use to query the server.
George Stocker
Ah okay, I missed the ManagementScope, this should work for me then. I'll give it a try
Bryan Denny
+1  A: 

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);
        }
}
Jason Irwin
This works for the machine that is executing the code, but what about for a remote server?
Bryan Denny
You could map a network drive, but it may be a little complicated to do programmatically
Jason Irwin
+1  A: 

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

DriveInfo c = new DriveInfo("C");
long cAvailableSpace = c.AvailableFreeSpace;
Bryan Denny
+1  A: 

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

anishmarokey