c#多个硬盘驱动器上的DirectoryInfo

时间:2013-09-07 13:12:32

标签: c#

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

4 个答案:

答案 0 :(得分:4)

目前尚不清楚您的要求,但我认为您可以使用Environment.GetLogicalDrives()方法。

  

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

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

在我的电脑中,输出是;

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

enter image description here

另外,请看一下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

答案 1 :(得分:1)

我认为你正在寻找

var drives = DriveInfo.GetDrives();
foreach (var di in drives)
{
    Console.WriteLine(di.Name);
}

答案 2 :(得分:0)

您可以使用DriveInfo.GetDrives Method

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

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

答案 3 :(得分: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.
}