No "AvailableSpace" property for Win32_LogicalDisk

C

cmsimmers

I'm trying to display the available space on a disk drive. This could
be tricky if the user choose a drive on which they are limited to a
quota. The "FreeSpace" property returns the total number of bytes on
the disk, which could be significantly larger than the space actually
available to the user.

I found documentation referring to VB scripting, which provided an
"AvailableSpace" property, in addition to the "FreeSpace" property.
http://groups.google.com/group/micr...st&q="AvailableSpace"&rnum=2#1417285711e11c94">Google
** sorry for this long URL, couldn't figure out how to make it a link

Is there an analog to this in C# / .NET WMI?

Here's some sample code in case I haven't been clear about what I'm
doing:

private String GetFreeSpace(String inVolume)
{
ManagementObject disk = new
ManagementObject("win32_logicaldisk.deviceid=\"" +
inVolume + "\"");
disk.Get();
int megabytes = (int)((Convert.ToInt64(disk["FreeSpace"]) /
1024) / 1024);

// does something like this property exist?
//int megabytes = (int)((Convert.ToInt64(disk["FreeSpace"])
/ 1024) / 1024);

return megabytes.ToString();
}
 
W

Willy Denoyette [MVP]

I'm trying to display the available space on a disk drive. This could
be tricky if the user choose a drive on which they are limited to a
quota. The "FreeSpace" property returns the total number of bytes on
the disk, which could be significantly larger than the space actually
available to the user.

I found documentation referring to VB scripting, which provided an
"AvailableSpace" property, in addition to the "FreeSpace" property.
http://groups.google.com/group/micr...st&q="AvailableSpace"&rnum=2#1417285711e11c94">Google
** sorry for this long URL, couldn't figure out how to make it a link

Is there an analog to this in C# / .NET WMI?

Here's some sample code in case I haven't been clear about what I'm
doing:

private String GetFreeSpace(String inVolume)
{
ManagementObject disk = new
ManagementObject("win32_logicaldisk.deviceid=\"" +
inVolume + "\"");
disk.Get();
int megabytes = (int)((Convert.ToInt64(disk["FreeSpace"]) /
1024) / 1024);

// does something like this property exist?
//int megabytes = (int)((Convert.ToInt64(disk["FreeSpace"])
/ 1024) / 1024);

return megabytes.ToString();
}



On W2K3 or higher you can use the win32_VolumeUserQuota to get at the user quota status.

Here is something that might get you started...
...
// Account "administrators" on "BOBSMACHINE"
string userAccount = "Win32_UserAccount.Domain=\"BOBSMACHINE\",Name=\"administrators\"";
ManagementObject uAcc = new ManagementObject(userAccount);
// Diskvolume C:\
string quotaVolume = @"Win32_QuotaSetting.VolumePath=" + "\"" + @"c:\\" +"\"";
RelatedObjectQuery relatedQuery = new RelatedObjectQuery
("associators of {" + quotaVolume +"}");
relatedQuery.RelatedClass = "win32_Volume";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(relatedQuery);
foreach(ManagementObject vol in searcher.Get())
{
string volPath = vol.Path.ToString().Replace("\\", "\\\\");
volPath = VolumeS.Replace("\"", "\\\"");
string classPath =
String.Format("Win32_VolumeUserQuota.Account=\"{0}\",Volume=\"{1}\"",
uAcc .Path.ToString().Replace("\"", "\\\""), VolumeS);
ManagementObject vuq = new ManagementObject(classPath);
Console.WriteLine("Account {0}, has {1} bytes used from {2} {3} {4}",
vuq["Account"].ToString(),
vuq["DiskSpaceUsed"].ToString(),
vuq["Limit"].ToString(),
vuq["Volume"].ToString(),
vuq["WarningLimit"].ToString());
}
}
 
C

cmsimmers

Willy -

Thanks for the sample code - very helpful. It makes sense that this
kind of data would be related to the specific user, since that's how
quotas are set up.

Cheri
I'm trying to display the available space on a disk drive. This could
be tricky if the user choose a drive on which they are limited to a
quota. The "FreeSpace" property returns the total number of bytes on
the disk, which could be significantly larger than the space actually
available to the user.

I found documentation referring to VB scripting, which provided an
"AvailableSpace" property, in addition to the "FreeSpace" property.
http://groups.google.com/group/micr...st&q="AvailableSpace"&rnum=2#1417285711e11c94">Google
** sorry for this long URL, couldn't figure out how to make it a link

Is there an analog to this in C# / .NET WMI?

Here's some sample code in case I haven't been clear about what I'm
doing:

private String GetFreeSpace(String inVolume)
{
ManagementObject disk = new
ManagementObject("win32_logicaldisk.deviceid=\"" +
inVolume + "\"");
disk.Get();
int megabytes = (int)((Convert.ToInt64(disk["FreeSpace"]) /
1024) / 1024);

// does something like this property exist?
//int megabytes = (int)((Convert.ToInt64(disk["FreeSpace"])
/ 1024) / 1024);

return megabytes.ToString();
}



On W2K3 or higher you can use the win32_VolumeUserQuota to get at the user quota status.

Here is something that might get you started...
...
// Account "administrators" on "BOBSMACHINE"
string userAccount = "Win32_UserAccount.Domain=\"BOBSMACHINE\",Name=\"administrators\"";
ManagementObject uAcc = new ManagementObject(userAccount);
// Diskvolume C:\
string quotaVolume = @"Win32_QuotaSetting.VolumePath=" + "\"" + @"c:\\" +"\"";
RelatedObjectQuery relatedQuery = new RelatedObjectQuery
("associators of {" + quotaVolume +"}");
relatedQuery.RelatedClass = "win32_Volume";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(relatedQuery);
foreach(ManagementObject vol in searcher.Get())
{
string volPath = vol.Path.ToString().Replace("\\", "\\\\");
volPath = VolumeS.Replace("\"", "\\\"");
string classPath =
String.Format("Win32_VolumeUserQuota.Account=\"{0}\",Volume=\"{1}\"",
uAcc .Path.ToString().Replace("\"", "\\\""), VolumeS);
ManagementObject vuq = new ManagementObject(classPath);
Console.WriteLine("Account {0}, has {1} bytes used from {2} {3} {4}",
vuq["Account"].ToString(),
vuq["DiskSpaceUsed"].ToString(),
vuq["Limit"].ToString(),
vuq["Volume"].ToString(),
vuq["WarningLimit"].ToString());
}
}
 
W

Willy Denoyette [MVP]

Willy -

Thanks for the sample code - very helpful. It makes sense that this
kind of data would be related to the specific user, since that's how
quotas are set up.

Specific users or user groups. Users quota's are only possible on Server OSses (W2K/W2K3),
on Client OSses you can only set quotas per group.

Willy.
 
Top