This blog post shows a bit of C# code to use the Windows Storage Management API (SM-API) classes that were introduced in Windows Server 2012 and Windows 8.
You can find a list of these classes at class described at http://msdn.microsoft.com/en-us/library/hh830612.aspx, including MSFT_PhysicalDisk, MSFT_StoragePool or MSFT_VirtualDisk.
I found a number of examples with the old interface using the old classes like Win32_Volume, but few good ones with the new classes like MSFT_Volume.
This is some simple C# code using console output. The main details to highlight here are the use of System.Management and how to specify the scope, which allows you to manage a remote computer.
Please note that you might need to enable WMI on the computer, which can be easily done with the command line “winrm quickconfig”.
Here is the source code, for the console application, which lists information about volumes and physical disks on the local machine.
using System;
using System.Text;
using System.Threading;
using System.Management;
namespace SMAPIQuery
{
class Program
{
static void Main(string[] args)
{
// Use the Storage management scope
ManagementScope scope = new ManagementScope("\\\\localhost\\ROOT\\Microsoft\\Windows\\Storage");
// Define the query for volumes
ObjectQuery query = new ObjectQuery("SELECT * FROM MSFT_Volume");
// create the search for volumes
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
// Get the volumes
ManagementObjectCollection allVolumes = searcher.Get();
// Loop through all volumes
foreach (ManagementObject oneVolume in allVolumes)
{
// Show volume information
if (oneVolume["DriveLetter"].ToString()[0] > ' ' )
{
Console.WriteLine("Volume '{0}' has {1} bytes total, {2} bytes available", oneVolume["DriveLetter"], oneVolume["Size"], oneVolume["SizeRemaining"]);
}
}
// Define the query for physical disks
query = new ObjectQuery("SELECT * FROM MSFT_PhysicalDisk");
// create the search for physical disks
searcher = new ManagementObjectSearcher(scope, query);
// Get the physical disks
ManagementObjectCollection allPDisks = searcher.Get();
// Loop through all physical disks
foreach (ManagementObject onePDisk in allPDisks)
{
// Show physical disk information
Console.WriteLine("Disk {0} is model {1}, serial number {2}", onePDisk["DeviceId"], onePDisk["Model"], onePDisk["SerialNumber"]);
}
Console.ReadLine();
}
}
}
Here is some sample output from this application:
Volume 'D' has 500104687616 bytes total, 430712184832 bytes available
Volume 'E' has 132018860032 bytes total, 110077665280 bytes available
Volume 'F' has 500105216000 bytes total, 356260683776 bytes available
Volume 'C' has 255690010624 bytes total, 71789502464 bytes available
Disk 2 is model SD , serial number
Disk 0 is model MTFDDAK256MAM-1K12, serial number 131109303905
Disk 3 is model 5AS , serial number 00000000e45ca01b30c1
Disk 1 is model ST9500325AS, serial number 6VEK9B89