Use file-scoped namespaces (#842)

This commit is contained in:
PhyxionNL
2022-10-09 13:22:57 +02:00
committed by GitHub
parent 3ac329f247
commit 58cef111a1
175 changed files with 34026 additions and 34204 deletions
@@ -9,90 +9,89 @@ using System.Management;
using System.Runtime.InteropServices;
using LibreHardwareMonitor.Interop;
namespace LibreHardwareMonitor.Hardware.Storage
namespace LibreHardwareMonitor.Hardware.Storage;
internal static class WindowsStorage
{
internal static class WindowsStorage
public static Storage.StorageInfo GetStorageInfo(string deviceId, uint driveIndex)
{
public static Storage.StorageInfo GetStorageInfo(string deviceId, uint driveIndex)
using SafeHandle handle = Kernel32.OpenDevice(deviceId);
if (handle?.IsInvalid != false)
return null;
var query = new Kernel32.STORAGE_PROPERTY_QUERY { PropertyId = Kernel32.STORAGE_PROPERTY_ID.StorageDeviceProperty, QueryType = Kernel32.STORAGE_QUERY_TYPE.PropertyStandardQuery };
if (!Kernel32.DeviceIoControl(handle,
Kernel32.IOCTL.IOCTL_STORAGE_QUERY_PROPERTY,
ref query,
Marshal.SizeOf(query),
out Kernel32.STORAGE_DEVICE_DESCRIPTOR_HEADER header,
Marshal.SizeOf<Kernel32.STORAGE_DEVICE_DESCRIPTOR_HEADER>(),
out _,
IntPtr.Zero))
{
using SafeHandle handle = Kernel32.OpenDevice(deviceId);
if (handle?.IsInvalid != false)
return null;
var query = new Kernel32.STORAGE_PROPERTY_QUERY { PropertyId = Kernel32.STORAGE_PROPERTY_ID.StorageDeviceProperty, QueryType = Kernel32.STORAGE_QUERY_TYPE.PropertyStandardQuery };
if (!Kernel32.DeviceIoControl(handle,
Kernel32.IOCTL.IOCTL_STORAGE_QUERY_PROPERTY,
ref query,
Marshal.SizeOf(query),
out Kernel32.STORAGE_DEVICE_DESCRIPTOR_HEADER header,
Marshal.SizeOf<Kernel32.STORAGE_DEVICE_DESCRIPTOR_HEADER>(),
out _,
IntPtr.Zero))
{
return null;
}
IntPtr descriptorPtr = Marshal.AllocHGlobal((int)header.Size);
try
{
if (!Kernel32.DeviceIoControl(handle, Kernel32.IOCTL.IOCTL_STORAGE_QUERY_PROPERTY, ref query, Marshal.SizeOf(query), descriptorPtr, header.Size, out _, IntPtr.Zero))
return null;
return new StorageInfo((int)driveIndex, descriptorPtr);
}
finally
{
Marshal.FreeHGlobal(descriptorPtr);
}
return null;
}
public static string[] GetLogicalDrives(int driveIndex)
IntPtr descriptorPtr = Marshal.AllocHGlobal((int)header.Size);
try
{
var list = new List<string>();
if (!Kernel32.DeviceIoControl(handle, Kernel32.IOCTL.IOCTL_STORAGE_QUERY_PROPERTY, ref query, Marshal.SizeOf(query), descriptorPtr, header.Size, out _, IntPtr.Zero))
return null;
try
return new StorageInfo((int)driveIndex, descriptorPtr);
}
finally
{
Marshal.FreeHGlobal(descriptorPtr);
}
}
public static string[] GetLogicalDrives(int driveIndex)
{
var list = new List<string>();
try
{
using var s = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DiskPartition " + "WHERE DiskIndex = " + driveIndex);
foreach (ManagementBaseObject o in s.Get())
{
using var s = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DiskPartition " + "WHERE DiskIndex = " + driveIndex);
foreach (ManagementBaseObject o in s.Get())
if (o is ManagementObject dp)
{
if (o is ManagementObject dp)
foreach (ManagementBaseObject ld in dp.GetRelated("Win32_LogicalDisk"))
{
foreach (ManagementBaseObject ld in dp.GetRelated("Win32_LogicalDisk"))
{
list.Add(((string)ld["Name"]).TrimEnd(':'));
}
list.Add(((string)ld["Name"]).TrimEnd(':'));
}
}
}
catch
{
// Ignored.
}
return list.ToArray();
}
catch
{
// Ignored.
}
private class StorageInfo : Storage.StorageInfo
{
public StorageInfo(int index, IntPtr descriptorPtr)
{
Kernel32.STORAGE_DEVICE_DESCRIPTOR descriptor = Marshal.PtrToStructure<Kernel32.STORAGE_DEVICE_DESCRIPTOR>(descriptorPtr);
Index = index;
Vendor = GetString(descriptorPtr, descriptor.VendorIdOffset);
Product = GetString(descriptorPtr, descriptor.ProductIdOffset);
Revision = GetString(descriptorPtr, descriptor.ProductRevisionOffset);
Serial = GetString(descriptorPtr, descriptor.SerialNumberOffset);
BusType = descriptor.BusType;
Removable = descriptor.RemovableMedia;
RawData = new byte[descriptor.Size];
Marshal.Copy(descriptorPtr, RawData, 0, RawData.Length);
}
return list.ToArray();
}
private static string GetString(IntPtr descriptorPtr, uint offset)
{
return offset > 0 ? Marshal.PtrToStringAnsi(new IntPtr(descriptorPtr.ToInt64() + offset))?.Trim() : string.Empty;
}
private class StorageInfo : Storage.StorageInfo
{
public StorageInfo(int index, IntPtr descriptorPtr)
{
Kernel32.STORAGE_DEVICE_DESCRIPTOR descriptor = Marshal.PtrToStructure<Kernel32.STORAGE_DEVICE_DESCRIPTOR>(descriptorPtr);
Index = index;
Vendor = GetString(descriptorPtr, descriptor.VendorIdOffset);
Product = GetString(descriptorPtr, descriptor.ProductIdOffset);
Revision = GetString(descriptorPtr, descriptor.ProductRevisionOffset);
Serial = GetString(descriptorPtr, descriptor.SerialNumberOffset);
BusType = descriptor.BusType;
Removable = descriptor.RemovableMedia;
RawData = new byte[descriptor.Size];
Marshal.Copy(descriptorPtr, RawData, 0, RawData.Length);
}
private static string GetString(IntPtr descriptorPtr, uint offset)
{
return offset > 0 ? Marshal.PtrToStringAnsi(new IntPtr(descriptorPtr.ToInt64() + offset))?.Trim() : string.Empty;
}
}
}