Uncategorized Home @ it-notebook.org

C#, Disk information for remote computers

(Kristofer Gafvert)

Purpose

This sample application will look up all computers in the file computers.txt, and find the logical drives, their size, name, free space and file system.

Usuage

Write the IP or name of the computer in the file computers.txt (hard coded name), one on a line. This file must be located in the same folder as RemoteDiskSpace.exe. Start the application by either double click the file, or run it from a command prompt. Do this on a client computer, there is no need to copy any files, or change anything on the remote computer(s).

Output

The application will create a html file (diskSpace.html), with information about disks, size of the disk, free space and file system in the same folder as RemoteDiskSpace.exe is located.

Example

The application in action

The code


     1: using System;
     2: using System.Management;
     3: using System.IO;
     4: 
     5: namespace RemoteDiskSpace
     6: {
     7:     /// <summary>
     8:     /// Summary description for Class1.
     9:     /// </summary>
    10:     class Class1
    11:     {
    12:         /// <summary>
    13:         /// The main entry point for the application.
    14:         /// </summary>
    15:         [STAThread]
    16:         static void Main(string[] args)
    17:         {
    18:             ConnectionOptions opt = new ConnectionOptions();
    19:             ObjectQuery oQuery = new ObjectQuery("SELECT Size, FreeSpace, Name, FileSystem FROM Win32_LogicalDisk WHERE DriveType = 3");
    20: 
    21:             StreamReader oReader = new StreamReader("computers.txt");
    22:             StreamWriter writer = new StreamWriter("diskSpace.html");
    23:             writer.WriteLine("<html><body><table border=\"0\" cellpadding=\"5\">");
    24:             writer.WriteLine(@"<tr>                                 
    25:                                     <th>Machine</th>
    26:                                     <th>Drive</th>
    27:                                     <th>Size GB</th>
    28:                                     <th>Free Space GB</th>
    29:                                     <th>Free Space %</th>
    30:                                     <th>FileSystem</th>
    31:                                 </tr>");
    32:             string sLine = String.Empty;
    33: 
    34:             while (sLine != null)
    35:             {
    36:                 sLine = oReader.ReadLine();
    37:                 
    38:                 if( sLine != null)
    39:                 {
    40:                     ManagementScope scope = new ManagementScope("\\\\" + sLine + "\\root\\cimv2", opt);
    41: 
    42:                     ManagementObjectSearcher moSearcher = new ManagementObjectSearcher(scope, oQuery);
    43:                     ManagementObjectCollection collection = moSearcher.Get();
    44:                     writer.WriteLine("<tr><td valign=\"top\">" + sLine + "</td></tr>");
    45:                     Console.Write("Trying " + sLine + "...");
    46:                     foreach(ManagementObject res in collection)
    47:                     {
    48:                         decimal size = Convert.ToDecimal(res["Size"])/1024/1024/1024;
    49:                         decimal freeSpace = Convert.ToDecimal(res["FreeSpace"])/1024/1024/1024;
    50:                         writer.WriteLine("<tr><td></td>");
    51:                         writer.WriteLine("<td>" + res["Name"] + "</td>");
    52:                         writer.WriteLine("<td>" + Decimal.Round(size, 2) + " GB </td>");
    53:                         writer.WriteLine("<td>" + Decimal.Round(freeSpace, 2) + " GB </td>");
    54:                         writer.WriteLine("<td>" + Decimal.Round(freeSpace/size, 2)*100 + "% </td>");
    55:                         writer.WriteLine("<td>" + res["FileSystem"] + "</td>");
    56:                         writer.WriteLine("</tr>");
    57:                     }
    58:                     Console.WriteLine("done!");
    59:                 }
    60: 
    61:             }
    62:             Console.WriteLine("Please open diskSpace.html for result");
    63:             writer.WriteLine("</table></body></html>");
    64:             writer.Close();
    65:             oReader.Close();
    66: 
    67:         }
    68:     }
    69: }
    70: 


Applies to [?]

C# 1.1
C# 2.0

See also

Download code and sample application
WMI, Win32_LogicalDisk class
System.Management Namespace