469,269 Members | 1,006 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,269 developers. It's quick & easy.

No "AvailableSpace" property for Win32_LogicalDisk

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();
}

Jan 20 '07 #1
3 5852
<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());
}
}

Jan 20 '07 #2
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

Willy Denoyette [MVP] wrote:
<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());
}
}
Jan 22 '07 #3
<cm*******@gmail.comwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
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.
Jan 22 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Edward Diener | last post: by
3 posts views Thread by Marty McFly | last post: by
27 posts views Thread by sklett | last post: by
reply views Thread by Memfis | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.