public static void DestroyDeviceInfoList(IntPtr hDevInfo) { bool success = DeviceInterfaceUtils.SetupDiDestroyDeviceInfoList(hDevInfo); if (!success) { int errorCode = Marshal.GetLastWin32Error(); string message = String.Format("Unable to destroy device info list, Win32 Error: {0}", errorCode); throw new IOException(message); } }
public static List <int> GetPhysicalDiskIndexList() { List <string> devicePathList = DeviceInterfaceUtils.GetDevicePathList(DeviceInterfaceUtils.DiskClassGuid); List <int> result = new List <int>(); foreach (string devicePath in devicePathList) { SafeFileHandle hDevice = HandleUtils.GetFileHandle(devicePath, FileAccess.Read, ShareMode.ReadWrite); STORAGE_DEVICE_NUMBER number = GetDeviceNumber(hDevice); hDevice.Close(); result.Add((int)number.DeviceNumber); } // We'll now sort the list based on disk number result.Sort(); return(result); }
// Great C++ Example of enumerating and locating specific attach storage devices: // http://code.msdn.microsoft.com/windowshardware/CppStorageEnum-90ad5fa9 public static List <string> GetDevicePathList(Guid deviceClassGuid) { IntPtr hDevInfo = GetClassDevices(deviceClassGuid); SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA(); deviceInterfaceData.cbSize = (uint)Marshal.SizeOf(typeof(SP_DEVICE_INTERFACE_DATA)); uint index = 0; List <string> result = new List <string>(); while (true) { bool success = DeviceInterfaceUtils.SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref deviceClassGuid, index, ref deviceInterfaceData); if (!success) { int errorCode = Marshal.GetLastWin32Error(); if (errorCode == (int)Win32Error.ERROR_NO_MORE_ITEMS) { break; } else { string message = String.Format("Unable to enumerate device interfaces, Win32 Error: {0}", errorCode); throw new IOException(message); } } SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData = DeviceInterfaceUtils.GetDeviceInterfaceDetail(hDevInfo, deviceInterfaceData); result.Add(deviceInterfaceDetailData.DevicePath); index++; } DestroyDeviceInfoList(hDevInfo); return(result); }