<cm*******@gmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
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/microsoft.public.scripting.vbscript/browse_frm/thread/3d6a9eee11db3e6f/1417285711e11c94?lnk=st&q=%22AvailableSpace%22&rnu m=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=\"a dministrators\"";
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());
}
}