0

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?

Ning Liu
  • 19
  • 4
  • 2
    Is your Visual Studio running elevated and/or as a different user than the explorer window where you double-click the executable? If you shift+right-click the EXE file and choose "Run as", can you reproduce the unexpected results? – Ben Voigt Jul 14 '20 at 19:34
  • Thank you @BenVoigt, you are right, I have run my VS as administrator, and directly run exe with my own user account. And because the GetDiskFreeSpaceEx get the free space for the current user, this is the reason why I got different result. Then as administrator how can I get disk info for certain user, instead of current user? Thank you! – Ning Liu Jul 15 '20 at 07:32
  • You'll have to use COM interop to create an instance of [`IDiskQuotaControl`](https://learn.microsoft.com/en-us/windows/win32/api/dskquota/nn-dskquota-idiskquotacontrol), then you can call `FindUserName` to get the quota details for a single user, or `CreateEnumUsers` to get quota information for all users. – Ben Voigt Jul 15 '20 at 15:10
  • There's an object intended for scripting exposed by the shell: https://learn.microsoft.com/en-us/windows/win32/shell/diskquotacontrol-object This one might be easier to use. – Ben Voigt Jul 15 '20 at 15:12

0 Answers0