-2

我需要一些可以检查的东西,System.IO.DirectoryInfo(@"d:\");但除了 CE F 之外的每个硬盘驱动器。如果我将来购买新硬盘驱动器,我需要一些稳定的东西。

4

4 回答 4

4

目前尚不清楚您问了什么,但我认为您可以使用Environment.GetLogicalDrives()方法。

返回包含当前计算机上逻辑驱动器名称的字符串数组。

string[] drives = Environment.GetLogicalDrives();
foreach (var drive in drives)
{
    Console.WriteLine(drive);
}

在我的电脑中,输出是;

C:\
D:\
Q:\
Y:\
Z:\

在此处输入图像描述

或者,看看DriveInfo.GetDrives方法。

检索计算机上所有逻辑驱动器的驱动器名称。

来自MSDN页面的示例;

    DriveInfo[] allDrives = DriveInfo.GetDrives();

    foreach (DriveInfo d in allDrives)
    {
        Console.WriteLine("Drive {0}", d.Name);
        Console.WriteLine("  File type: {0}", d.DriveType);
        if (d.IsReady == true)
        {
            Console.WriteLine("  Volume label: {0}", d.VolumeLabel);
            Console.WriteLine("  File system: {0}", d.DriveFormat);
            Console.WriteLine(
                "  Available space to current user:{0, 15} bytes", 
                d.AvailableFreeSpace);

            Console.WriteLine(
                "  Total available space:          {0, 15} bytes",
                d.TotalFreeSpace);

            Console.WriteLine(
                "  Total size of drive:            {0, 15} bytes ",
                d.TotalSize);
        }

您可以获得这些信息;

Drive C:\
  File type: Fixed
  Volume label:
  File system: NTFS
  Available space to current user:   447202275328 bytes
  Total available space:             447202275328 bytes
  Total size of drive:               500105216000 bytes
Drive D:\
  File type: CDRom
Drive Q:\
  File type: Network
  Volume label: HDS4
  File system: NTFS
  Available space to current user:     4053897216 bytes
  Total available space:               4053897216 bytes
  Total size of drive:              1188893290496 bytes
Drive Y:\
  File type: Network
  Volume label: Data
  File system: NTFS
  Available space to current user:     5525561344 bytes
  Total available space:               5525561344 bytes
  Total size of drive:                72958230528 bytes
Drive Z:\
  File type: Network
  Volume label: HDS3
  File system: NTFS
  Available space to current user:   147224600576 bytes
  Total available space:             147224600576 bytes
  Total size of drive:              1230321479680 bytes
于 2013-09-07T13:16:32.557 回答
1

我想你正在寻找

var drives = DriveInfo.GetDrives();
foreach (var di in drives)
{
    Console.WriteLine(di.Name);
}
于 2013-09-07T13:19:20.363 回答
0

你要来一次吗?

public static IEnumerable<DirectoryInfo> GetSelectedDirectoryInformation(String argDirectoryName, IEnumerable<String> argExcludeDrives)
{
   foreach(DriveInfo di in DriveInfo.GetDrives())
   {
      if ((argExcludedDrives == null) || (!argExcludedDrives.Contains(di.Name)))
      {
         string directory = Path.Combine(di.Name,argDirectoryName); 
         if (DirectoryExists(directory))
         {
            yield return new DirectoryInfo(directory);
         }
      }
   }
}

现在你可以一口气做

foreach(DirectoryInfo di in GetSelectedDirectoryInformation("FTP",new string[] {@"C:\",@"E:\",@"F:\"}))
{
   // Do something with it.
}
于 2013-09-07T14:13:09.283 回答
0

您可以使用DriveInfo.GetDrives 方法检索计算机上所有逻辑驱动器的驱动器名称

此方法返回DriveInfo数组。使用属性DriveInfo.DriveType,您还可以排除 CDRom 或网络驱动器等。

于 2013-09-07T13:19:40.630 回答