달력

32024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

[C#] Network Driver Check

.NET/C# 2011. 8. 11. 10:49
[link] http://www.publicjoe.f9.co.uk/csharp/snip/snip010.html


C# Snippets


  1. How do I retreive the total space and free space for network drives?

  1. Here is how to get 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;

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

I have created a little application to demo in C# Express.

A similar application to get the space on C: drive can be found here.

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 C# Express 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.


References

For more information on the SelectQuery class visit MSDN at microsoft here.

For more information on the System.Management Namespace visit MSDN at microsoft here.

For more information on the ManagementObject class visit MSDN at microsoft here.

For more information on the ManagementObjectSearcher class visit MSDN at microsoft here.

For more information on the Win32_LogicalDisk class visit MSDN at microsoft here.

Back to Snippets.



Posted by tornado
|