<cm*******@gmai l.comwrote in message
news:11******** **************@ q2g2000cwa.goog legroups.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.c om/group/microsoft.publi c.scripting.vbs cript/browse_frm/thread/3d6a9eee11db3e6 f/1417285711e11c9 4?lnk=st&q=%22A vailableSpace%2 2&rnum=2#141728 5711e11c94">Goo gle
** 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(St ring inVolume)
{
ManagementObjec t disk = new
ManagementObjec t("win32_logica ldisk.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.ToStr ing();
}
On W2K3 or higher you can use the win32_VolumeUse rQuota to get at the user quota status.
Here is something that might get you started...
...
// Account "administrators " on "BOBSMACHIN E"
string userAccount = "Win32_UserAcco unt.Domain=\"BO BSMACHINE\",Nam e=\"administrat ors\"";
ManagementObjec t uAcc = new ManagementObjec t(userAccount);
// Diskvolume C:\
string quotaVolume = @"Win32_QuotaSe tting.VolumePat h=" + "\"" + @"c:\\" +"\"";
RelatedObjectQu ery relatedQuery = new RelatedObjectQu ery
("associator s of {" + quotaVolume +"}");
relatedQuery.Re latedClass = "win32_Volu me";
ManagementObjec tSearcher searcher = new ManagementObjec tSearcher(relat edQuery);
foreach(Managem entObject vol in searcher.Get())
{
string volPath = vol.Path.ToStri ng().Replace("\ \", "\\\\");
volPath = VolumeS.Replace ("\"", "\\\"");
string classPath =
String.Format(" Win32_VolumeUse rQuota.Account= \"{0}\",Volume= \"{1}\"",
uAcc .Path.ToString( ).Replace("\"", "\\\""), VolumeS);
ManagementObjec t vuq = new ManagementObjec t(classPath);
Console.WriteLi ne("Account {0}, has {1} bytes used from {2} {3} {4}",
vuq["Account"].ToString(),
vuq["DiskSpaceU sed"].ToString(),
vuq["Limit"].ToString(),
vuq["Volume"].ToString(),
vuq["WarningLim it"].ToString());
}
}