Search code examples
c#volumevds

How to get free space on a disk volume without letter assigned?


I'm trying to find out free space information about volumes. Those with letters assigned are fine (GetDiskFreeSpaceEx). I've also connected to VDS (Virtual Disk Service) and retrieved something called AvailableAllocationUnits (A) and AllocationUnitSize (B), where A*B = free size shown by Windows, but B is 4096, so this is not an exact number in bytes.

  1. How is it possible to determine this without VDS?
  2. Is there a more precise way (in bytes)?

Solution

  • On Windows, you could execute the following commands and parse the output:

    vssadmin list volumes
    

    This gives:

    C:\Windows\system32>vssadmin list volumes
    vssadmin 1.1 - Volume Shadow Copy Service administrative command-line tool
    (C) Copyright 2001-2013 Microsoft Corp.
    
    Volume path: \\?\Volume{66c6160d-60cc-11e3-824b-806e6f6e6963}\
        Volume name: \\?\Volume{66c6160d-60cc-11e3-824b-806e6f6e6963}\
    Volume path: D:\
        Volume name: \\?\Volume{66c6160f-60cc-11e3-824b-806e6f6e6963}\
    Volume path: C:\
        Volume name: \\?\Volume{66c6160e-60cc-11e3-824b-806e6f6e6963}\
    

    Then Execute

    fsutil volume diskfree
    

    Which gives:

    C:\Users\MC>fsutil volume diskfree \\?\Volume{66c6160e-60cc-11e3-824b-806e6f6e6963}\
    Total # of free bytes        : 47826694144
    Total # of bytes             : 255691059200
    Total # of avail free bytes  : 47826694144
    

    To read output of a shell process, you can read the standard output

    string output = proc.StandardOutput.ReadToEnd();
    

    DISCLAIMER: Yes I know its not exactly the cleanest way, but it is a way. As I'm not aware of an API for accessing such low level info.