Portable Tool for an Ideal Windows Setup
C# activatorbloatware-removalcsharpdebloatdebloaterdisable-servicesmonitoring-systemportableprivacy-toolsregistry-tweakssystem-tuningsystem-utilitytelemetrytweakerwindows-10windows-11windows-debloatwindows-defenderwindows-optimizationwindows-tweaks
add low-level disk type identification
Greedeksff8260ba9bea
diff --git a/.Source/GTweak/GTweak.csproj b/.Source/GTweak/GTweak.csprojindex 3c090e44..fec797c0 100644--- a/.Source/GTweak/GTweak.csproj+++ b/.Source/GTweak/GTweak.csproj@@ -133,6 +133,7 @@<Compile Include="Core\ViewModel\PakagesViewModel.cs" /><Compile Include="Core\ViewModel\ServicesViewModel.cs" /><Compile Include="Core\ViewModel\SystemViewModel.cs" />+ <Compile Include="Utilities\Configuration\DiskTypeIdentifier.cs" /><Compile Include="Utilities\Configuration\HardwareData.cs" /><Compile Include="Utilities\Maintenance\ClearingMemory.cs" /><Compile Include="Utilities\Maintenance\RegistryExporter.cs" />diff --git a/.Source/GTweak/Utilities/Configuration/DiskTypeIdentifier.cs b/.Source/GTweak/Utilities/Configuration/DiskTypeIdentifier.csnew file mode 100644index 00000000..3aaf385a--- /dev/null+++ b/.Source/GTweak/Utilities/Configuration/DiskTypeIdentifier.cs@@ -0,0 +1,143 @@+using System;+using System.Runtime.InteropServices;+using Microsoft.Win32.SafeHandles;++namespace GTweak.Utilities.Configuration+{+ internal static class DiskTypeIdentifier+ {+ private const uint IOCTL_STORAGE_QUERY_PROPERTY = 0x2D1400;+ private const uint FILE_SHARE_READ = 1;+ private const uint FILE_SHARE_WRITE = 2;+ private const uint OPEN_EXISTING = 3;++ private enum STORAGE_PROPERTY_ID+ {+ StorageDeviceProperty = 0,+ StorageDeviceSeekPenaltyProperty = 7+ }++ private enum STORAGE_QUERY_TYPE+ {+ PropertyStandardQuery = 0+ }++ private enum STORAGE_BUS_TYPE : uint+ {+ BusTypeUnknown = 0,+ BusTypeScsi = 1,+ BusTypeAtapi = 2,+ BusTypeAta = 3,+ BusTypeUsb = 7,+ BusTypeRAID = 8,+ BusTypeSas = 10,+ BusTypeSata = 11,+ BusTypeSd = 12,+ BusTypeVirtual = 14,+ BusTypeFileBackedVirtual = 15,+ BusTypeNvme = 17+ }++ [StructLayout(LayoutKind.Sequential)]+ private struct STORAGE_PROPERTY_QUERY+ {+ public STORAGE_PROPERTY_ID PropertyId;+ public STORAGE_QUERY_TYPE QueryType;+ [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]+ public byte[] AdditionalParameters;+ }++ [StructLayout(LayoutKind.Sequential)]+ private struct DEVICE_SEEK_PENALTY_DESCRIPTOR+ {+ public uint Version;+ public uint Size;+ [MarshalAs(UnmanagedType.U1)]+ public bool IncursSeekPenalty;+ }++ [StructLayout(LayoutKind.Sequential)]+ private struct STORAGE_DEVICE_DESCRIPTOR+ {+ public uint Version;+ public uint Size;+ public byte DeviceType;+ public byte DeviceTypeModifier;+ [MarshalAs(UnmanagedType.U1)] public bool RemovableMedia;+ [MarshalAs(UnmanagedType.U1)] public bool CommandQueueing;+ public uint VendorIdOffset;+ public uint ProductIdOffset;+ public uint ProductRevisionOffset;+ public uint SerialNumberOffset;+ public STORAGE_BUS_TYPE BusType;+ public uint RawPropertiesLength;+ }++ [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]+ private static extern SafeFileHandle CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);++ [DllImport("kernel32.dll", SetLastError = true)]+ private static extern bool DeviceIoControl(SafeFileHandle hDevice, uint dwIoControlCode, ref STORAGE_PROPERTY_QUERY lpInBuffer, int nInBufferSize, IntPtr lpOutBuffer, int nOutBufferSize, out uint lpBytesReturned, IntPtr lpOverlapped);++ [DllImport("kernel32.dll", SetLastError = true)]+ private static extern bool DeviceIoControl(SafeFileHandle hDevice, uint dwIoControlCode, ref STORAGE_PROPERTY_QUERY lpInBuffer, int nInBufferSize, out DEVICE_SEEK_PENALTY_DESCRIPTOR lpOutBuffer, int nOutBufferSize, out uint lpBytesReturned, IntPtr lpOverlapped);++ internal static string GetStorageKind(string physicalDrivePath)+ {+ try+ {+ using SafeFileHandle handle = CreateFile(physicalDrivePath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);++ if (handle.IsInvalid)+ {+ return HardwareData.DiskTypeLabels.Unspecified;+ }++ STORAGE_PROPERTY_QUERY query = new STORAGE_PROPERTY_QUERY+ {+ PropertyId = STORAGE_PROPERTY_ID.StorageDeviceProperty,+ QueryType = STORAGE_QUERY_TYPE.PropertyStandardQuery,+ AdditionalParameters = new byte[1]+ };++ IntPtr buffer = Marshal.AllocHGlobal(1024);+ try+ {+ if (DeviceIoControl(handle, IOCTL_STORAGE_QUERY_PROPERTY, ref query, Marshal.SizeOf(query), buffer, 1024, out _, IntPtr.Zero))+ {+ STORAGE_DEVICE_DESCRIPTOR descriptor = Marshal.PtrToStructure<STORAGE_DEVICE_DESCRIPTOR>(buffer);++ bool isSSD = false;+ query.PropertyId = STORAGE_PROPERTY_ID.StorageDeviceSeekPenaltyProperty;++ if (DeviceIoControl(handle, IOCTL_STORAGE_QUERY_PROPERTY, ref query, Marshal.SizeOf(query), out DEVICE_SEEK_PENALTY_DESCRIPTOR seek, Marshal.SizeOf<DEVICE_SEEK_PENALTY_DESCRIPTOR>(), out _, IntPtr.Zero))+ {+ isSSD = !seek.IncursSeekPenalty;+ }++ return descriptor.BusType switch+ {+ STORAGE_BUS_TYPE.BusTypeUnknown => HardwareData.DiskTypeLabels.Unspecified,+ STORAGE_BUS_TYPE.BusTypeNvme => HardwareData.DiskTypeLabels.NVMe,+ STORAGE_BUS_TYPE.BusTypeUsb => HardwareData.DiskTypeLabels.USB,+ STORAGE_BUS_TYPE.BusTypeSd => HardwareData.DiskTypeLabels.SD,+ STORAGE_BUS_TYPE.BusTypeVirtual => HardwareData.DiskTypeLabels.VHD,+ STORAGE_BUS_TYPE.BusTypeFileBackedVirtual => HardwareData.DiskTypeLabels.VHDX,+ _ => isSSD ? HardwareData.DiskTypeLabels.SSD : HardwareData.DiskTypeLabels.HDD+ };+ }+ }+ finally+ {+ Marshal.FreeHGlobal(buffer);+ }++ return HardwareData.DiskTypeLabels.Unspecified;+ }+ catch+ {+ return HardwareData.DiskTypeLabels.Unspecified;+ }+ }+ }+}diff --git a/.Source/GTweak/Utilities/Configuration/HardwareData.cs b/.Source/GTweak/Utilities/Configuration/HardwareData.csindex 9678551c..691f2511 100644--- a/.Source/GTweak/Utilities/Configuration/HardwareData.cs+++ b/.Source/GTweak/Utilities/Configuration/HardwareData.cs@@ -1,4 +1,4 @@-using System.Windows.Media;+using System.Windows.Media;namespace GTweak.Utilities.Configuration{@@ -56,5 +56,18 @@ namespace GTweak.Utilities.Configurationinternal static bool Nvidia { get; set; } = default;internal static bool Realtek { get; set; } = default;}++ internal static class DiskTypeLabels+ {+ internal const string Unspecified = "(Unspecified)";+ internal const string SCM = "(SCM)";+ internal const string HDD = "(HDD)";+ internal const string SSD = "(SSD)";+ internal const string NVMe = "(NVMe SSD)";+ internal const string USB = "(USB-Media)";+ internal const string SD = "(SD-Card)";+ internal const string VHD = "(VHD)";+ internal const string VHDX = "(VHDX)";+ }}}\ No newline at end of filediff --git a/.Source/GTweak/Utilities/Configuration/SystemDiagnostics.cs b/.Source/GTweak/Utilities/Configuration/SystemDiagnostics.csindex 5efd099c..4c3c2cd4 100644--- a/.Source/GTweak/Utilities/Configuration/SystemDiagnostics.cs+++ b/.Source/GTweak/Utilities/Configuration/SystemDiagnostics.cs@@ -48,6 +48,20 @@ namespace GTweak.Utilities.Configurationinternal static bool isIPAddressFormatValid = false, isMsftAvailable = false;+ private static readonly (object[] Keys, string Type)[] MediaTypeMap = new (object[] Keys, string Type)[]+ {+ (new object[] { (ushort)3, "Removable Media" }, DiskTypeLabels.HDD),+ (new object[] { (ushort)4, "Fixed hard disk media" }, DiskTypeLabels.SSD),+ (new object[] { (ushort)5, "Unspecified" }, DiskTypeLabels.SCM)+ };++ private static readonly Dictionary<ushort, string> BusTypeMap = new Dictionary<ushort, string>()+ {+ { 7, DiskTypeLabels.USB },+ { 12, DiskTypeLabels.SD },+ { 17, DiskTypeLabels.NVMe }+ };+internal static (string Code, string Region) GetCurrentSystemLang(){CultureInfo culture = CultureInfo.CurrentUICulture;@@ -128,7 +142,6 @@ namespace GTweak.Utilities.ConfigurationAudioDevice = audio;NetworkAdapter = netAdapter;break;-}}@@ -290,7 +303,7 @@ namespace GTweak.Utilities.Configurationforeach (var managementObj in new ManagementObjectSearcher(@"root\cimv2", "select Manufacturer, Name, Caption, Description, Tag, Capacity, ConfiguredClockSpeed, Speed, SMBIOSMemoryType from Win32_PhysicalMemory", new EnumerationOptions { ReturnImmediately = true }).Get()){string speedData = new[] { "ConfiguredClockSpeed", "Speed" }.Select(prop => managementObj[prop] != null ? Convert.ToString(managementObj[prop]) : null).FirstOrDefault(info => !string.IsNullOrEmpty(info) && info != "0");- string data = new[] { "Manufacturer", "Name", "Caption", "Description", "Tag" }.Select(prop => managementObj[prop] as string).FirstOrDefault(value => !string.IsNullOrWhiteSpace(value) || !value.Equals("Unknown", StringComparison.OrdinalIgnoreCase)) ?? string.Empty;+ string data = new[] { "Manufacturer", "Name", "Caption", "Description", "Tag" }.Select(prop => managementObj[prop] as string).FirstOrDefault(value => !string.IsNullOrWhiteSpace(value) || !value.Equals("Unspecified", StringComparison.OrdinalIgnoreCase)) ?? string.Empty;string memoryType = (uint)managementObj["SMBIOSMemoryType"] switch{2 => "DRAM",@@ -338,50 +351,67 @@ namespace GTweak.Utilities.Configuration/// <summary>/// The MSFT_PhysicalDisk class may be missing or malfunctioning; in such cases, it will be replaced by the universal Win32_DiskDrive class./// </summary>+ ///private string GetStorageDevices(){StringBuilder result = new StringBuilder();- static string GetStorageType(object mediaType)+ static string GetStorageType(object mediaType, string deviceId, ushort busType, string interfaceType){- (object[] Keys, string Type)[] map = new (object[] Keys, string Type)[]+ string storageType = MediaTypeMap.FirstOrDefault(x => x.Keys.Contains(mediaType)).Type ?? DiskTypeLabels.Unspecified;++ if (isMsftAvailable){- (new object[] { (ushort)3, "Removable Media" }, "(HDD)"),- (new object[] { (ushort)4, "Fixed hard disk media" }, "(SSD)"),- (new object[] { (ushort)5, "Unknown" }, "(SCM)")- };- return map.FirstOrDefault(x => x.Keys.Contains(mediaType)).Type ?? "(Unspecified)";+ if (BusTypeMap.TryGetValue(busType, out var busStorageType))+ {+ storageType = busStorageType;+ }+ }+ else+ {+ if ((storageType == DiskTypeLabels.Unspecified || storageType == DiskTypeLabels.HDD) && (string.IsNullOrEmpty(interfaceType) || interfaceType.IndexOf("USB", StringComparison.OrdinalIgnoreCase) >= 0))+ {+ return DiskTypeLabels.USB;+ }+ }++ if (storageType != DiskTypeLabels.Unspecified)+ {+ string lowLevelType = DiskTypeIdentifier.GetStorageKind(deviceId);+ if (!string.IsNullOrEmpty(lowLevelType) && lowLevelType != DiskTypeLabels.Unspecified)+ {+ return lowLevelType;+ }+ }++ if (storageType == DiskTypeLabels.Unspecified && !string.IsNullOrEmpty(deviceId))+ {+ return DiskTypeIdentifier.GetStorageKind(deviceId);+ }++ return storageType;}if (isMsftAvailable){- foreach (var managementObj in new ManagementObjectSearcher(@"root\microsoft\windows\storage", "select FriendlyName, Model, Description, MediaType, Size, BusType from MSFT_PhysicalDisk", new EnumerationOptions { ReturnImmediately = true }).Get())+ foreach (var managementObj in new ManagementObjectSearcher(@"root\microsoft\windows\storage", "select DeviceId, FriendlyName, Model, Description, MediaType, Size, BusType from MSFT_PhysicalDisk", new EnumerationOptions { ReturnImmediately = true }).Get()){string data = new[] { "FriendlyName", "Model", "Description" }.Select(prop => managementObj[prop] as string).FirstOrDefault(info => !string.IsNullOrEmpty(info)) ?? string.Empty;ushort mediaType = managementObj["MediaType"] != null ? (ushort)managementObj["MediaType"] : (ushort)0;- string storageType = GetStorageType(mediaType);-- if (storageType == "(Unspecified)" && ((ushort)managementObj["BusType"]) == 7)- {- storageType = "(Media-Type)";- }+ ushort busType = managementObj["BusType"] != null ? (ushort)managementObj["BusType"] : (ushort)0;+ string storageType = GetStorageType(mediaType, $@"\\.\PhysicalDrive{managementObj["DeviceId"]?.ToString() ?? "0"}", busType, null);result.AppendLine($"{SizeCalculationHelper((ulong)managementObj["Size"])} [{data}] {storageType}");}}else{- foreach (var managementObj in new ManagementObjectSearcher(@"root\cimv2", "select Model, Caption, Size, MediaType, InterfaceType from Win32_DiskDrive", new EnumerationOptions { ReturnImmediately = true }).Get())+ foreach (var managementObj in new ManagementObjectSearcher(@"root\cimv2", "select DeviceID, Model, Caption, Size, MediaType, InterfaceType from Win32_DiskDrive", new EnumerationOptions { ReturnImmediately = true }).Get()){string data = new[] { "Model", "Caption" }.Select(prop => managementObj[prop] as string).FirstOrDefault(info => !string.IsNullOrEmpty(info)) ?? string.Empty;string mediaType = managementObj["MediaType"] as string ?? string.Empty;- string storageType = GetStorageType(mediaType);string interfaceType = managementObj["InterfaceType"] as string ?? string.Empty;-- if ((storageType == "(Unspecified)" || storageType == "(HDD)") && (string.IsNullOrEmpty(interfaceType) || interfaceType.IndexOf("USB", StringComparison.OrdinalIgnoreCase) >= 0))- {- storageType = "(Media-Type)";- }+ string storageType = GetStorageType(mediaType, managementObj["DeviceID"] as string ?? string.Empty, default, interfaceType);result.AppendLine($"{SizeCalculationHelper((ulong)managementObj["Size"])} [{data}] {storageType}");}