disk size, space with UNC

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If I have a drive letter I can get the the drive info from WMI like this

string ltrDr=theSharePath.Substring(0,2); // must be in form of "E:
string wackyQuery =string.Format("Win32_LogicalDisk.DeviceID=\"{0}\"",ltrDr)
ManagementObject DIdisk =new ManagementObject(wackyQuery)
DIdisk.Get()
qcShareSize= Convert.ToDouble(DIdisk["FreeSpace"])/1000000000; // in GigaByte

but if I just have the UNC what then ? What does WMI expect ??? I have not found any examples to map to drive ltr directly or simply use the UNC path.

Thanks Andrew:
 
andrew,

If you have a UNC name, then you can not use the Win32_LogicalDisk
class. The reason for this is that this class corresponds to mapped drives
on the system.

If you want to get the disk free space of a network share, you will have
to call the GetDiskFreeSpace API through the P/Invoke layer.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

andrewcw said:
If I have a drive letter I can get the the drive info from WMI like this:

string ltrDr=theSharePath.Substring(0,2); // must be in form of "E:"
string wackyQuery =string.Format("Win32_LogicalDisk.DeviceID=\"{0}\"",ltrDr);
ManagementObject DIdisk =new ManagementObject(wackyQuery);
DIdisk.Get();
qcShareSize= Convert.ToDouble(DIdisk["FreeSpace"])/1000000000; // in GigaBytes

but if I just have the UNC what then ? What does WMI expect ??? I have
not found any examples to map to drive ltr directly or simply use the UNC
path.
 
Thanks - I was trying to stay as much as possible within managed code. While waiting I put this code together - although it is not as direct ...it does get me the drive ltr & then I can use WMI for size. Seems an odd oversight in the class to not have such a function..

public string getDrvLtrFromUNC(string UNCPath
{ // get all the named drives then see whose ProviderNam
// matches the UNCPath, then return that drive letter ( form of "e: " - 2 chars
// empty string would be an unmapped or invalid drive path
string retVal=""
string thisDescrip=""
string thisUNCroot=""
string ltrDr=""
string wackyQuery=""
string [] logDrives=Directory.GetLogicalDrives()

for (int i = 0; i<logDrives.GetLength(0);i++

ltrDr=logDrives.Substring(0,2)
wackyQuery =string.Format("Win32_LogicalDisk.DeviceID=\"{0}\"",ltrDr)
ManagementObject moDrive =new ManagementObject(wackyQuery)
thisDescrip= moDrive.Properties["Description"].Value.ToString()
if (thisDescrip.IndexOf("Network")>-1
{
thisUNCroot = moDrive.Properties["ProviderName"].Value.ToString()
if (thisUNCroot.ToLower().IndexOf(UNCPath.ToLower())>-1

retVal = ltrDr
break

} // end of a network driv

} // end of for loo

return retVal
}
 
Andrew,

It's not an oversight, it's the design of the class. A logical drive
and a network share are two separate things which sometimes are the same (if
that makes sense).

If you want to get this information in a consistent manner, then use the
GetDiskFreeSpace API call, or some other API which allows both logical
drives and UNC names.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

andrewcw said:
Thanks - I was trying to stay as much as possible within managed code.
While waiting I put this code together - although it is not as direct ...it
does get me the drive ltr & then I can use WMI for size. Seems an odd
oversight in the class to not have such a function...
public string getDrvLtrFromUNC(string UNCPath)
{ // get all the named drives then see whose ProviderName
// matches the UNCPath, then return that drive letter ( form of "e: " - 2 chars)
// empty string would be an unmapped or invalid drive path.
string retVal="";
string thisDescrip="";
string thisUNCroot="";
string ltrDr="";
string wackyQuery="";
string [] logDrives=Directory.GetLogicalDrives();

for (int i = 0; i<logDrives.GetLength(0);i++)
{
ltrDr=logDrives.Substring(0,2);
wackyQuery =string.Format("Win32_LogicalDisk.DeviceID=\"{0}\"",ltrDr);
ManagementObject moDrive =new ManagementObject(wackyQuery);
thisDescrip= moDrive.Properties["Description"].Value.ToString();
if (thisDescrip.IndexOf("Network")>-1)
{
thisUNCroot = moDrive.Properties["ProviderName"].Value.ToString();
if (thisUNCroot.ToLower().IndexOf(UNCPath.ToLower())>-1)
{
retVal = ltrDr;
break;
}
} // end of a network drive

} // end of for loop

return retVal;
}
 
Thanks for reminding me of the differences. Hopefully this functionality will be pulled into the framework.
 
Back
Top