如何在 C# 中检索磁盘信息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/412632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:23:52  来源:igfitidea点击:

How do I retrieve disk information in C#?

c#disk

提问by leo

I would like to access information on the logical drives on my computer using C#. How should I accomplish this? Thanks!

我想使用 C# 访问有关我计算机上的逻辑驱动器的信息。我应该如何做到这一点?谢谢!

采纳答案by Vinko Vrsalovic

For most information, you can use the DriveInfoclass.

对于大多数信息,您可以使用DriveInfo类。

using System;
using System.IO;

class Info {
    public static void Main() {
        DriveInfo[] drives = DriveInfo.GetDrives();
        foreach (DriveInfo drive in drives) {
            //There are more attributes you can use.
            //Check the MSDN link for a complete example.
            Console.WriteLine(drive.Name);
            if (drive.IsReady) Console.WriteLine(drive.TotalSize);
        }
    }
}

回答by bruno conde

Check the DriveInfoClass and see if it contains all the info that you need.

检查DriveInfo类,看看它是否包含您需要的所有信息。

回答by Foozinator

What about mounted volumes, where you have no drive letter?

没有驱动器号的挂载卷呢?

foreach( ManagementObject volume in 
             new ManagementObjectSearcher("Select * from Win32_Volume" ).Get())
{
  if( volume["FreeSpace"] != null )
  {
    Console.WriteLine("{0} = {1} out of {2}",
                  volume["Name"],
                  ulong.Parse(volume["FreeSpace"].ToString()).ToString("#,##0"),
                  ulong.Parse(volume["Capacity"].ToString()).ToString("#,##0"));
  }
}

回答by mmushtaq

If you want to get information for single/specific driveat your local machine. You can do it as follow using DriveInfoclass:

如果您想在本地机器上获取单个/特定驱动器的信息。您可以使用DriveInfo类执行以下操作:

//C Drive Path, this is useful when you are about to find a Drive root from a Location Path.
string path = "C:\Windows";

//Find its root directory i.e "C:\"
string rootDir = Directory.GetDirectoryRoot(path);

//Get all information of Drive i.e C
DriveInfo driveInfo = new DriveInfo(rootDir); //you can pass Drive path here e.g   DriveInfo("C:\")

long availableFreeSpace = driveInfo.AvailableFreeSpace;
string driveFormat = driveInfo.DriveFormat;
string name = driveInfo.Name;
long totalSize = driveInfo.TotalSize;

回答by JoanComasFdz

In ASP .NET Core 3.1, if you want to get code that works both on windows and on linux, you can get your drives as follows:

在 ASP .NET Core 3.1 中,如果您想获得在 windows 和 linux 上都能运行的代码,您可以按如下方式获取驱动器:

var drives = DriveInfo
    .GetDrives()
    .Where(d => d.DriveType == DriveType.Fixed)
    .Where(d => d.IsReady
    .ToArray();

If you don't apply both wheres, you are going to get many drives if you run the code in linux (e.g. "/dev", "/sys", "/etc/hosts", etc.).

如果您不同时应用这两个 wheres,那么如果您在 linux 中运行代码(例如“/dev”、“/sys”、“/etc/hosts”等),您将获得许多驱动器。

This is specially useful when developing an app to work in a Linux Docker container.

这在开发应用程序以在 Linux Docker 容器中工作时特别有用。