I want to get the available disk free space when the disk quota is set. I have limited my D disk to 500Gb with disk quota, D disk is limited to 500Gb. Without the setup of disk quota, the free space of D is 803Gb D disk without disk quota
And from this question Get free disk space, I got the code from sasha_gud, and adapt it into a console application.
class Program
{
// Pinvoke for API function
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);
static void Main(string[] args)
{
var lFolderPath = @"D:\Imagepools12";
ulong lAvailableBytes, lTotalFree, dummy1;
if (GetDiskFreeSpaceEx(lFolderPath, out lAvailableBytes, out dummy1, out lTotalFree))
{
Console.WriteLine($"The available free space of {lFolderPath} is {lAvailableBytes} and total free is {lTotalFree}");
}
Console.ReadKey();
}
}
When I debug it in the Visual Studio 2019, I got the result of the lAvailableBytes and lTotalFree, both equal to 862827331584. Debug in VS get wrong lAvailableBytes
When I directly go the Debug folder and double click the executable, then I get the correct lAvailableBytes Run exe get correct lAvailableBytes
Could you please help me to figure out why debug in VS cannot get correct lAvailabelBytes? And how to fix it? Thank you!
Update: Thank you @Ben Voigt for figuring out that I have run my VS as administrator and by directly run exe as my own user account, this is why I got different result. Then how could I as administrator get disk information for certain user, instead of current user?