Network Drive Free Space

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
C# Code Snippets

C# Source Code Bank

See also …
edit

This C# code snippet allows you to obtain the free space of a networked drive using System.Management and WMI. It's important to note that the free space could be incorrect when user-quotas are applied.

using System;
using System.Management;
 
class NetworkSpace
{
  static void Main(string[] args)
  {
    SelectQuery query = new SelectQuery(
        "select name, FreeSpace from win32_logicaldisk where drivetype=4");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
 
    foreach (ManagementObject mo in searcher.Get())
    {
      Console.WriteLine("Drive letter is: {0}", mo["name"]);
      Console.WriteLine("Drive's free space is: {0}", mo["FreeSpace"]);
    }
 
    // Here to stop app from closing
    Console.WriteLine("\nPress Return to exit.");
    Console.Read();
  }
}

Note... In order to use the System.Management namespace in .NET 2, you will need to add a reference to the System.Management.dll. This can be done in the IDE by right-clicking on the project in the solution explorer and choosing Add Reference... from the list. The dll is on the first tab towards the end of the list of items.


MSDN references


Personal tools