5

Is there a programmatic API for determining available space on NAS storage from a UNC path? I looked through the WMI documentation and it wasn't clear that this is possible.

A code example and references to the relevant API calls would be much appreciated.

ANeves
  • 5,951
  • 3
  • 36
  • 63
LBushkin
  • 125,412
  • 32
  • 212
  • 261
  • in case a mod sees this, this should probably be marked a duplicate of 61037184. Yes this question came first but that one has an actual implementation of the only upvoted answer here and bringing in dll's is non-trivial so those details are pertinent. – Chris Rudd Dec 29 '21 at 00:04

2 Answers2

4

In the windows API, GetFreeDiskSpaceEx seems to be the method to use, which works on UNC paths according to the MSDN docs.

Community
  • 1
  • 1
dsolimano
  • 8,600
  • 3
  • 47
  • 62
  • GetDiskFreeSpaceEx isn't available in VB.Net. How do you achieve finding free space on a UNC path in VB.Net (not C!) – Tym Dec 18 '20 at 13:29
  • @Tym you can call into Windows dlls using the .Net DllImport infrastructure – dsolimano Dec 24 '20 at 03:48
-3

Using this example on how to get the UNC path, you could just return the FreeSpace property, I've modified the code below:

ManagementPath path = new ManagementPath(@"\" + System.Environment.MachineName + @"\root\cimv2");
ObjectQuery query = new ObjectQuery("select * from Win32_LogicalDisk WHERE DriveType = 4");
ManagementScope scope = new ManagementScope(path, new ConnectionOptions());
ManagementObjectSearcher search = new ManagementObjectSearcher(scope, query);

foreach (ManagementObject o in search.Get())
{
    Console.WriteLine(o.Properties["FreeSpace"].Value.ToString());
}
taylonr
  • 10,604
  • 5
  • 36
  • 65
  • 4
    I think this only works if the UNC path is already 'mapped' to a logical disk (with drive letter, e.g. Z:). What if you want to check free space on a server that is not mapped? – CyberMonk May 06 '11 at 16:19