15

Possible Duplicate:
Programmatically determining space available from UNC Path

I'm trying to find a function that I can call from C# to retrieve that information. This is what I have tried so far:

String folder = "z:\myfolder"; // It works
folder = "\\mycomputer\myfolder"; // It doesn't work

System.IO.DriveInfo drive = new System.IO.DriveInfo(folder);
System.IO.DriveInfo a = new System.IO.DriveInfo(drive.Name);
long HDPercentageUsed = 100 - (100 * a.AvailableFreeSpace / a.TotalSize);

This works ok, but only if I pass a drive letter. Is there a way of retrieving the free space by passing a whole path?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
LEM
  • 825
  • 6
  • 16
  • 31
  • look at [this](http://stackoverflow.com/questions/2965729/how-to-get-network-drive-size-in-c-sharp-without-map-drive). answered by Mitch Wheat *I believe you will need to call GetDiskFreeSpace (Win32 API) via P/Invoke to get the disk free space of a UNC network drive.* [C# GetDiskFreeSpace UNC](http://www.sergey.co.uk/ShowThread.aspx?ID=4563&AspxAutoDetectCookieSupport=1) – Hossein Narimani Rad Jan 22 '13 at 18:15

1 Answers1

27

Try to use the winapi function GetDiskFreeSpaceEx:

[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
   out ulong lpFreeBytesAvailable,
   out ulong lpTotalNumberOfBytes,
   out ulong lpTotalNumberOfFreeBytes);

ulong FreeBytesAvailable;
ulong TotalNumberOfBytes;
ulong TotalNumberOfFreeBytes;

bool success = GetDiskFreeSpaceEx(@"\\mycomputer\myfolder",
                                  out FreeBytesAvailable,
                                  out TotalNumberOfBytes,
                                  out TotalNumberOfFreeBytes);
if(!success)
    throw new System.ComponentModel.Win32Exception();

Console.WriteLine("Free Bytes Available:      {0,15:D}", FreeBytesAvailable);
Console.WriteLine("Total Number Of Bytes:     {0,15:D}", TotalNumberOfBytes);
Console.WriteLine("Total Number Of FreeBytes: {0,15:D}", TotalNumberOfFreeBytes);
rekire
  • 47,260
  • 30
  • 167
  • 264
  • This didn't work until I stumbled on using "C:\\" `success = GetDiskFreeSpaceEx("C:\\", out FreeBytesAvailable, ...` – J. Chris Compton Sep 21 '15 at 02:50
  • 1
    @J.Chris you missed the @ sign before the first quote, that makes the masking obsolete. – rekire Sep 21 '15 at 04:09
  • Thank you @rekire ! I actually didn't have that problem because I was using `folder = @"c:\\";` and using the variable folder. I simplified the code and missed the "@". The trouble I had was trying to use strings like @"\\L9F603\Home" i.e. the computer name (not my real computer name) slash drive or directory. If someone can post how to specify the computer name or a specific directory I would like to know that (but don't have to have it for this project). – J. Chris Compton Sep 22 '15 at 18:52
  • 2
    Does anyone know of a way to do this in pure Dot Net without an API call? DriveInfo seems to only work with drives, not UNC paths. – Robert Bratton Jan 17 '17 at 14:04
  • @RobertBratton regarding pure .net no idea I didn't write a single line c# for years :( However the code above contains a UNC path, isn't it? – rekire Jan 17 '17 at 14:58