0

I have a bunch of iSCSI drives mounted as NFTS folders (to avoid exhausting all the drive letters) acting as a mini SAN, and I would like to get the information about their free space. The basic reason is to get warnings when space gets below a certain threshold, as a part of a scheduled task which does a bunch of other checks.

Is there a way to do it, preferably using C# (through WMI, P/Invoke, or whatever)? Of course, any scripting solution would also be great, as I can probably invoke it anyway (PowerShell)? I tried the optimistic route first, using DriveInfo initialized with using such a path, but it simply returns information about the root volume instead of the mount. I've also tried enumerating stuff like Win32_DiskPartition, Win32_LogicalDisk and Win32_MappedLogicalDisk but didn't get those drives at all.

vgru
  • 49,838
  • 16
  • 120
  • 201
  • This is apparently [possible with PowerShell](http://www.powershelladmin.com/wiki/PowerShell_Get-MountPointData_Cmdlet#Showing_Used.2FFree_Space_On_Mount_Points). Looks like it relies on the `Win32_Volume` WMI class. – Frédéric Hamidi Oct 23 '14 at 15:20
  • @FrédéricHamidi: hey, thanks a lot! `Win32_Volume` indeed lists those drives. You can add this as an answer. – vgru Oct 23 '14 at 15:25

1 Answers1

0

As @FrédéricHamidi explained, Win32_Volume class in the WMI Storage Volume Provider shows correct space information about mounted volumes.

Usage example (C#) would be something like:

// iSCSI drive mounted in a NTFS folder
var ntfsPath = @"x:\iscsi\volume";

// it's good to know that backspaces must be escaped in WMI queries
var cmd = string.Format(
    "SELECT * FROM Win32_Volume WHERE Name LIKE '{0}%'", 
    ntfsPath.Replace(@"\", @"\\"));

using (var searcher = new ManagementObjectSearcher(cmd))
{
    foreach (ManagementObject queryObj in searcher.Get())
    {
        var name = (string)queryObj["Name"];
        var freeSpaceInBytes = (ulong)queryObj["FreeSpace"];
    }
}
vgru
  • 49,838
  • 16
  • 120
  • 201