Im having issues on the following code when converting to Int64 or any big number. Any help is appreciated.
public static void GetDiskspace(string MachineName, string DriveLetter)
{
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope("\\\\" + MachineName + "\\root\\cimv2",
options);
scope.Connect();
SelectQuery query1 = new SelectQuery("Select * from Win32_LogicalDisk");
ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(scope, query1);
ManagementObjectCollection queryCollection1 = searcher1.Get();
foreach (ManagementObject mo in queryCollection1)
{
// Display Logical Disks information
if (mo["Name"].Equals(DriveLetter.ToUpper()+":"))
if (((Int64)mo["FreeSpace"]) < (1024 * 1024 * 100)) //100GB <<----HERE IS WHERE I GET THE ERROR
{
Console.WriteLine(MachineName + " ALERT. LOW SPACE ON DRIVE " + mo["Name"]);
Console.WriteLine();
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();
}
EDIT: changed “Size” to “FreeSpace”.
It’s
ulongboxed intoobject. So you can only unbox it toulong(i.e.UInt64).Have a look at Win32_LogicalDisk class
An extract from
Win32_LogicalDiskclass properties description:Size
P.S.:
1) Actually if you do need to have
Int64, you can do the following cast:2) You could read this article by Eric Lippert, clarifying the essence of the exception in question.