gpt4 book ai didi

c# - 如何在C#中获得可用的Total HardDrive和已使用的cpace?

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:05 31 4
gpt4 key购买 nike

我有下面的代码来获取单个驱动器的信息,但是我想要整个HDD信息。总大小和已用或可用空间

public List<DriveInformation> GetDriveInfo()
{
List<DriveInformation> info = new List<DriveInformation>();
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
DriveInformation temp = new DriveInformation();
temp.driveName = drive.Name;
temp.driveSize = drive.TotalSize.ToString();
temp.availableSize = drive.TotalFreeSpace.ToString();
info.Add(temp);
}
return info;
}


如果我有未分配的驱动器,则驱动器信息无法显示该空间。
例如:我有500GB硬盘,每个分区有2个(C&D)分区200,然后此功能仅提供有关C和D的信息。我还希望使用或未分配未分配空间详细信息(即另外100GB)。

最佳答案

尝试这个。

    public static void GetDiskspace()
{
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope("\\\\localhost\\root\\cimv2",
options);
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
SelectQuery query1 = new SelectQuery("Select * from Win32_LogicalDisk");

ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(scope, query1);
ManagementObjectCollection queryCollection1 = searcher1.Get();

foreach (ManagementObject m in queryCollection)
{
// Display the remote computer information

Console.WriteLine("Computer Name : {0}", m["csname"]);
Console.WriteLine("Windows Directory : {0}", m["WindowsDirectory"]);
Console.WriteLine("Operating System: {0}", m["Caption"]);
Console.WriteLine("Version: {0}", m["Version"]);
Console.WriteLine("Manufacturer : {0}", m["Manufacturer"]);
Console.WriteLine();
}

foreach (ManagementObject mo in queryCollection1)
{
// Display Logical Disks information

Console.WriteLine(" Disk Name : {0}", mo["Name"]);
Console.WriteLine(" Disk Size : {0}", mo["Size"]);
Console.WriteLine(" FreeSpace : {0}", mo["FreeSpace"]);
Console.WriteLine(" Disk DeviceID : {0}", mo["DeviceID"]);
Console.WriteLine(" Disk VolumeName : {0}", mo["VolumeName"]);
Console.WriteLine(" Disk SystemName : {0}", mo["SystemName"]);
Console.WriteLine("Disk VolumeSerialNumber : {0}", mo["VolumeSerialNumber"]);
Console.WriteLine();
}
string line;
line = Console.ReadLine();
}

关于c# - 如何在C#中获得可用的Total HardDrive和已使用的cpace?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25737148/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com