吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 969|回复: 7
收起左侧

[求助] C#如何获取硬盘中所有分区信息(DriveInfo.GetDrives()只能获取可见分区,系统隐藏...

[复制链接]
ilovecomputer66 发表于 2023-1-10 23:04
C#如何获取硬盘中所有分区信息(DriveInfo.GetDrives()只能获取可见分区,windows 8 以上系统,那些安装时隐藏分区,或者是手工删除盘符的,都无法获取到

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| ilovecomputer66 发表于 2023-1-11 09:15
继续求一下,第三方分区软件不知道调用什么系统API获取的
g5cc 发表于 2023-1-11 09:23
g5cc 发表于 2023-1-11 09:31
[C#] 纯文本查看 复制代码
public override List<Drive> GetDriveList()
{
	List<Drive> list = new List<Drive>();
	string queryString = this.UseAsteriskInWMI ? "SELECT * FROM Win32_DiskDrive" : "SELECT Caption, Description, DeviceID, FirmwareRevision, Index, Manufacturer, Model, Name, Partitions, SerialNumber, Size FROM Win32_DiskDrive";
	List<Drive> result;
	using (ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(this._managementScope, queryString, this._enumerationOptions))
	{
		foreach (ManagementBaseObject managementBaseObject in managementObjectSearcher.Get())
		{
			Drive drive = new Drive
			{
				Caption = HardwareInfoRetrieval.GetPropertyString(managementBaseObject["Caption"]),
				Description = HardwareInfoRetrieval.GetPropertyString(managementBaseObject["Description"]),
				FirmwareRevision = HardwareInfoRetrieval.GetPropertyString(managementBaseObject["FirmwareRevision"]),
				Index = HardwareInfoRetrieval.GetPropertyValue<uint>(managementBaseObject["Index"]),
				Manufacturer = HardwareInfoRetrieval.GetPropertyString(managementBaseObject["Manufacturer"]),
				Model = HardwareInfoRetrieval.GetPropertyString(managementBaseObject["Model"]),
				Name = HardwareInfoRetrieval.GetPropertyString(managementBaseObject["Name"]),
				Partitions = HardwareInfoRetrieval.GetPropertyValue<uint>(managementBaseObject["Partitions"]),
				SerialNumber = HardwareInfoRetrieval.GetPropertyString(managementBaseObject["SerialNumber"]),
				Size = HardwareInfoRetrieval.GetPropertyValue<ulong>(managementBaseObject["Size"])
			};
			string str = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='";
			object obj = managementBaseObject["DeviceID"];
			string queryString2 = str + ((obj != null) ? obj.ToString() : null) + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition";
			using (ManagementObjectSearcher managementObjectSearcher2 = new ManagementObjectSearcher(this._managementScope, queryString2, this._enumerationOptions))
			{
				foreach (ManagementBaseObject managementBaseObject2 in managementObjectSearcher2.Get())
				{
					Partition partition = new Partition
					{
						Bootable = HardwareInfoRetrieval.GetPropertyValue<bool>(managementBaseObject2["Bootable"]),
						BootPartition = HardwareInfoRetrieval.GetPropertyValue<bool>(managementBaseObject2["BootPartition"]),
						Caption = HardwareInfoRetrieval.GetPropertyString(managementBaseObject2["Caption"]),
						Description = HardwareInfoRetrieval.GetPropertyString(managementBaseObject2["Description"]),
						DiskIndex = HardwareInfoRetrieval.GetPropertyValue<uint>(managementBaseObject2["DiskIndex"]),
						Index = HardwareInfoRetrieval.GetPropertyValue<uint>(managementBaseObject2["Index"]),
						Name = HardwareInfoRetrieval.GetPropertyString(managementBaseObject2["Name"]),
						PrimaryPartition = HardwareInfoRetrieval.GetPropertyValue<bool>(managementBaseObject2["PrimaryPartition"]),
						Size = HardwareInfoRetrieval.GetPropertyValue<ulong>(managementBaseObject2["Size"]),
						StartingOffset = HardwareInfoRetrieval.GetPropertyValue<ulong>(managementBaseObject2["StartingOffset"])
					};
					string str2 = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='";
					object obj2 = managementBaseObject2["DeviceID"];
					string queryString3 = str2 + ((obj2 != null) ? obj2.ToString() : null) + "'} WHERE AssocClass = Win32_LogicalDiskToPartition";
					using (ManagementObjectSearcher managementObjectSearcher3 = new ManagementObjectSearcher(this._managementScope, queryString3, this._enumerationOptions))
					{
						foreach (ManagementBaseObject managementBaseObject3 in managementObjectSearcher3.Get())
						{
							Volume item = new Volume
							{
								Caption = HardwareInfoRetrieval.GetPropertyString(managementBaseObject3["Caption"]),
								Compressed = HardwareInfoRetrieval.GetPropertyValue<bool>(managementBaseObject3["Compressed"]),
								Description = HardwareInfoRetrieval.GetPropertyString(managementBaseObject3["Description"]),
								FileSystem = HardwareInfoRetrieval.GetPropertyString(managementBaseObject3["FileSystem"]),
								FreeSpace = HardwareInfoRetrieval.GetPropertyValue<ulong>(managementBaseObject3["FreeSpace"]),
								Name = HardwareInfoRetrieval.GetPropertyString(managementBaseObject3["Name"]),
								Size = HardwareInfoRetrieval.GetPropertyValue<ulong>(managementBaseObject3["Size"]),
								VolumeName = HardwareInfoRetrieval.GetPropertyString(managementBaseObject3["VolumeName"]),
								VolumeSerialNumber = HardwareInfoRetrieval.GetPropertyString(managementBaseObject3["VolumeSerialNumber"])
							};
							partition.VolumeList.Add(item);
						}
						drive.PartitionList.Add(partition);
					}
				}
				list.Add(drive);
			}
		}
		result = list;
	}
	return result;
}
 楼主| ilovecomputer66 发表于 2023-1-11 15:40
g5cc 发表于 2023-1-11 09:31
[mw_shl_code=csharp,true]public override List GetDriveList()
{
        List list = new List();

能提供完整代码么?Driver类没有
 楼主| ilovecomputer66 发表于 2023-1-11 16:25
g5cc 发表于 2023-1-11 09:31
[mw_shl_code=csharp,true]public override List GetDriveList()
{
        List list = new List();

对于不可见分区,执行 string queryString3 = str2 + ((obj2 != null) ? obj2.ToString() : null) + "'} WHERE AssocClass = Win32_LogicalDiskToPartition";

这句,都是获取不到任何结果的。最终效果,还是只获取了 C、D 2个可见的,隐藏分区拿不到
g5cc 发表于 2023-1-11 16:43
ilovecomputer66 发表于 2023-1-11 16:25
对于不可见分区,执行 string queryString3 = str2 + ((obj2 != null) ? obj2.ToString() : null) + "'}  ...

看我上面的图,不可见分区和没有盘符的分区是可以显示出来的。
 楼主| ilovecomputer66 发表于 2023-1-11 16:49
g5cc 发表于 2023-1-11 16:43
看我上面的图,不可见分区和没有盘符的分区是可以显示出来的。

我实测我的电脑。我SSD,安装win10,执行到获取分区信息的query3,系统隐藏分区,都没有信息,就C D 盘最终有

而且你的截图是用第三方库,我说的是你贴的那段不完整代码的,补全后的运行结果
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 04:20

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表