重新创建Windows磁盘管理 [英] Recreating Windows Disk Management
问题描述
您好我想重新创建Windows管理以使每个hdd连接到系统和该磁盘每个分区的分区大小,可用空间和字母,到目前为止我得到每个磁盘,分区大小,但坚持获取字母 和自由空间,试图使用
Win32_LogicalDisk,但没有什么我可以与分区或磁盘相关联,也与Win32_Volume相同的事情
以下代码到目前为止:
ManagementObjectSearcher wmiService4 = new ManagementObjectSearcher(" select * from Win32_Volume");
foreach(wmiService4.Get()中的ManagementObject obj3)
{
Console.WriteLine(" DriveLetter:" + obj3 [" DriveLetter"]);
Console.WriteLine(" DeviceID:" + obj3 [" DeviceID"]);
Console.WriteLine(" Label:" + obj3 [" Label"]);
Console.WriteLine(" Name:" + obj3 [" Name"]);
Console.WriteLine();
}
int z = 0;
ManagementObjectSearcher wmiService = new ManagementObjectSearcher("从Win32_DiskDrive中选择索引,媒体类型,模型,分区,大小,状态,其中MediaType ='可移动媒体'或MediaType ='外部硬盘媒体'或MediaType ='固定硬盘媒体"");
foreach(wmiService.Get()中的ManagementObject obj)
{
string id = obj [" Index"]。ToString();
Console.WriteLine(" Index:" + id);
Console.WriteLine(" MediaType:" + obj [" MediaType"]);
Console.WriteLine(" Model:" + obj [" Model"]);
Console.WriteLine(" Partitions:" + obj [" Partitions"]);
double size =((Convert.ToDouble(obj [" Size"])/ 1024)/ 1024)/ 1024; // GB
Console.WriteLine(" Size:" + Math。轮(大小,1));
Console.WriteLine(" Status:" + obj [" Status"]);
Console.WriteLine();
ManagementObjectSearcher wmiService2 = new ManagementObjectSearcher("从Win32_DiskPartition中选择Bootable,BootPartition,DiskIndex,Index,PrimaryPartition,Size,DeviceID,其中DiskIndex ='" + id +"'");
foreach(wmiService2.Get()中的ManagementObject obj2)
{
Console.WriteLine(" Bootable:" + obj2 [" Bootable"]);
Console.WriteLine(" BootPartition:" + obj2 [" BootPartition"]);
Console.WriteLine(" DiskIndex:" + obj2 [" DiskIndex"]);
Console.WriteLine(" Index:" + obj2 [" Index"]);
Console.WriteLine(" PrimaryPartition:" + obj2 [" PrimaryPartition"]);
double size2 =((Convert.ToDouble(obj2 [" Size"])/ 1024)/ 1024)/ 1024; // GB
Console.WriteLine(" Size:" + Math。圆形(2号,1号));
Console.WriteLine(" DeviceID:" + obj2 [" DeviceID"]);
Console.WriteLine();
string zz = obj2 [" DeviceID"]。ToString();
ManagementObjectSearcher wmiService3 = new ManagementObjectSearcher(" select * from Win32_LogicalDisk");
foreach(wmiService3.Get()中的ManagementObject obj3)
{
Console.WriteLine(" FreeSpace:" + obj3 [" FreeSpace"]);
Console.WriteLine(" DeviceID:" + obj3 [" DeviceID"]);
Console.WriteLine();
}
}
}
Windows磁盘管理如何完成,如何获取我需要的信息?感谢您的帮助。
您使用
Win32_DiskPartition 获取分区信息。示例PowerShell代码段包含在链接页面中。
但是,上次我检查时仍建议您使用Windows API对分区进行更改,因为并非所有需要的方法都可用于WMI对象
Hello I want recreate windows management to get every hdd connected to system and that disk each partition with partition size, free space and letter, so far I get each disk, partition size, but stuck on getting letter and free space, Tried to use Win32_LogicalDisk but there is nothing what I could associate with partition or disk, also same thing with Win32_Volume
Code below of what I got so far:
ManagementObjectSearcher wmiService4 = new ManagementObjectSearcher("select * from Win32_Volume"); foreach (ManagementObject obj3 in wmiService4.Get()) { Console.WriteLine("DriveLetter: " + obj3["DriveLetter"]); Console.WriteLine("DeviceID: " + obj3["DeviceID"]); Console.WriteLine("Label: " + obj3["Label"]); Console.WriteLine("Name: " + obj3["Name"]); Console.WriteLine(); } int z = 0; ManagementObjectSearcher wmiService = new ManagementObjectSearcher("select Index,MediaType,Model,Partitions,Size,Status from Win32_DiskDrive where MediaType = 'Removable media' or MediaType = 'External hard disk media' or MediaType = 'Fixed hard disk media'"); foreach (ManagementObject obj in wmiService.Get()) { string id = obj["Index"].ToString(); Console.WriteLine("Index: " + id); Console.WriteLine("MediaType: " + obj["MediaType"]); Console.WriteLine("Model: " + obj["Model"]); Console.WriteLine("Partitions: " + obj["Partitions"]); double size = ((Convert.ToDouble(obj["Size"]) / 1024) / 1024) / 1024;//GB Console.WriteLine("Size: " + Math.Round(size,1)); Console.WriteLine("Status: " + obj["Status"]); Console.WriteLine(); ManagementObjectSearcher wmiService2 = new ManagementObjectSearcher("select Bootable,BootPartition,DiskIndex,Index,PrimaryPartition,Size,DeviceID from Win32_DiskPartition where DiskIndex = '" + id+"'"); foreach (ManagementObject obj2 in wmiService2.Get()) { Console.WriteLine("Bootable: " + obj2["Bootable"]); Console.WriteLine("BootPartition: " + obj2["BootPartition"]); Console.WriteLine("DiskIndex: " + obj2["DiskIndex"]); Console.WriteLine("Index: " + obj2["Index"]); Console.WriteLine("PrimaryPartition: " + obj2["PrimaryPartition"]); double size2 = ((Convert.ToDouble(obj2["Size"]) / 1024) / 1024) / 1024;//GB Console.WriteLine("Size: " + Math.Round(size2, 1)); Console.WriteLine("DeviceID: " + obj2["DeviceID"]); Console.WriteLine(); string zz = obj2["DeviceID"].ToString(); ManagementObjectSearcher wmiService3 = new ManagementObjectSearcher("select * from Win32_LogicalDisk"); foreach (ManagementObject obj3 in wmiService3.Get()) { Console.WriteLine("FreeSpace: " + obj3["FreeSpace"]); Console.WriteLine("DeviceID: " + obj3["DeviceID"]); Console.WriteLine(); } } }
How Windows Disk management done, how they get info that I need? Thanks for help.
You use Win32_DiskPartition to obtain partition information. Sample PowerShell snippet is included in linked page.
However, last time I checked you're still recommended to use Windows API to make changes to partitions as not all needed methods are available on WMI objects.
这篇关于重新创建Windows磁盘管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!