Hello. I need to find the available space for a given path (could be a network path) with a quota applied, but when I use the following code, it gives me the available space on the entire drive. For instance, I have a drive E: that has 500 GB of available space. But I have user folders on this drive and each user has a 2 GB quota. For instance, when I use this function giving the path "E:\Users\Greg" , it should return that 2 GB is free, but instead it returns 500 GB. I have verified that the quota is working in that it does not allow me to create more than 2 GB of files in that directory. Does anyone have a solution to this problem? Any help is greatly appreciated. Using C#.
Code:
using System; using System.IO; using System.Reflection; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; public class MainClass { [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern bool GetDiskFreeSpaceEx(string lpDirectoryName, out ulong lpFreeBytesAvailable, out ulong lpTotalNumberOfBytes, out ulong lpTotalNumberOfFreeBytes); public static long getFreeSpace(string path) { ulong freeBytesAvail; ulong totalNumOfBytes; ulong totalNumOfFreeBytes; if (!GetDiskFreeSpaceEx(path, out freeBytesAvail, out totalNumOfBytes, out totalNumOfFreeBytes)) { Console.Error.WriteLine("Error occurred: {0}", Marshal.GetExceptionForHR(Marshal.GetLastWin32Error()).Message); return 0; } else { return Convert.toInt64(freeBytesAvail / (1024 * 1024)); } } }
Comment