I have written code that reads in a list of computers and retrieves infomation regarding free HD space using WMI. No matter what remote computers are in the list, I always get info for my localhost. The entries in the list are formated like this:
\\\\computer1
\\\\computer2
Here is my Code:
using System;
using System.Management;
using System.IO;
namespace CheckDiskSpace
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string[] computers = getList("C:\\list.txt");
foreach (string s in computers)
{
CheckDisks(s);
}
Console.ReadLine();
}
static string[] getList(string path)
{
StreamReader txtStream = File.OpenText(path);
int counter = 0;
string input = null;
while ((input = txtStream.ReadLine()) != null)
{
counter = counter + 1;
}
txtStream.Close();
Console.WriteLine("There are " + counter + " servers in the list");
string[] list = new string[counter];
input = null;
counter = 0;
txtStream = File.OpenText(path);
while ((input = txtStream.ReadLine()) != null)
{
list[counter] = input;
counter = counter + 1;
}
return list;
}
static void CheckDisks(string remotePC)
{
long mb = 1048576; //bytes in a Mb
ConnectionOptions oConn = new ConnectionOptions();
System.Management.ManagementScope oMs = new System.Management.ManagementScope(remotePC, oConn);
System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select FreeSpace, Size, Name from Win32_LogicalDisk where DriveType=3");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs,oQuery);
ManagementObjectCollection oReturnCollection = oSearcher.Get();
double freespace = 0;
double usedspace = 0;
double totalspace = 0;
double percentused = 0;
double percentfree = 0;
Console.WriteLine("********************************");
Console.WriteLine("** " + remotePC + " Hard Disks **");
Console.WriteLine("********************************");
foreach (ManagementObject oReturn in oReturnCollection)
{
Console.WriteLine("Name : " + oReturn["Name"].ToString());
totalspace = Convert.ToInt64(oReturn["Size"])/mb;
freespace = Convert.ToInt64(oReturn["FreeSpace"])/mb;
usedspace = (totalspace - freespace)/mb;
percentused = (usedspace/totalspace) * 100;
percentfree = (freespace/totalspace) * 100;
Console.WriteLine("Free Space: " + freespace + " Mb");
}
oConn = null;
oMs = null;
oQuery = null;
oSearcher = null;
}
}
}
Thanks for the help.
\\\\computer1
\\\\computer2
Here is my Code:
using System;
using System.Management;
using System.IO;
namespace CheckDiskSpace
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string[] computers = getList("C:\\list.txt");
foreach (string s in computers)
{
CheckDisks(s);
}
Console.ReadLine();
}
static string[] getList(string path)
{
StreamReader txtStream = File.OpenText(path);
int counter = 0;
string input = null;
while ((input = txtStream.ReadLine()) != null)
{
counter = counter + 1;
}
txtStream.Close();
Console.WriteLine("There are " + counter + " servers in the list");
string[] list = new string[counter];
input = null;
counter = 0;
txtStream = File.OpenText(path);
while ((input = txtStream.ReadLine()) != null)
{
list[counter] = input;
counter = counter + 1;
}
return list;
}
static void CheckDisks(string remotePC)
{
long mb = 1048576; //bytes in a Mb
ConnectionOptions oConn = new ConnectionOptions();
System.Management.ManagementScope oMs = new System.Management.ManagementScope(remotePC, oConn);
System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select FreeSpace, Size, Name from Win32_LogicalDisk where DriveType=3");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs,oQuery);
ManagementObjectCollection oReturnCollection = oSearcher.Get();
double freespace = 0;
double usedspace = 0;
double totalspace = 0;
double percentused = 0;
double percentfree = 0;
Console.WriteLine("********************************");
Console.WriteLine("** " + remotePC + " Hard Disks **");
Console.WriteLine("********************************");
foreach (ManagementObject oReturn in oReturnCollection)
{
Console.WriteLine("Name : " + oReturn["Name"].ToString());
totalspace = Convert.ToInt64(oReturn["Size"])/mb;
freespace = Convert.ToInt64(oReturn["FreeSpace"])/mb;
usedspace = (totalspace - freespace)/mb;
percentused = (usedspace/totalspace) * 100;
percentfree = (freespace/totalspace) * 100;
Console.WriteLine("Free Space: " + freespace + " Mb");
}
oConn = null;
oMs = null;
oQuery = null;
oSearcher = null;
}
}
}
Thanks for the help.