【问题标题】:Finding a specific folder on all drives [closed]在所有驱动器上查找特定文件夹[关闭]
【发布时间】:2016-06-23 06:05:50
【问题描述】:

我正在尝试制作一个需要找到特定文件夹才能工作的程序,通常是“C:\Program Files (x86)\Steam\SteamApps\common\Team Fortress 2”,但也可能是安装在不同的驱动器上或单独安装在“程序文件”中。有没有办法检查每个可用的驱动器来找到它,如果它根本找不到它(例如,E:\Games\Team Fortress)将布尔值或其他东西(boolCanFindFolder)标记为 false,以便我可以提示用户进入自己的目录,然后检查它是否存在?

【问题讨论】:

  • 您正在寻找DriveInfo.GetDrives()Directory.Exists()
  • 所以也许我可以得到所有驱动器并单独检查每个驱动器?您能否大致介绍一下我将如何进行设置?干杯!
  • @MaxBox - 我们不是代码编写服务。在这种情况下,谷歌是你的朋友。您应该能够找到大量参考如何做到这一点。当您尝试实现它并遇到困难时,请回到这里并问我们一个显示您的代码的问题,我们很乐意提供帮助。这就是 Stack Overflow 的意义所在 - 帮助解决特定编程问题。
  • 您正在寻找一个循环,或带有FirstOrDefault() 的LINQ。
  • @MaxBox 使用drives = DriveInfo.GetDrives()。设置foreach(var drive in drives)。然后循环你的猜测(例如Program Files (x86)\Steam\SteamApps\common\Team Fortress 2) - foreach (var expectedPath in expectedPaths) 并做类似Directory.Exists(Path.Combine(drive, expectedPath))

标签: c# directory


【解决方案1】:

要获取计算机上可用驱动器的列表,
您可以使用 System.IO.DriveInfo 类。

这里有一个例子来解决你的问题:

  string[] candidatePaths= {@"Program Files",@"Program Files (x86)\Steam\SteamApps\common\Team Fortress 2"};

    var path = "";
    foreach(var drive in DriveInfo.GetDrives())
    {     
       foreach(var candPath in candidatePaths)
       {
         if(Directory.Exists(Path.Combine(drive, candPath ))
         {
           found = true;
           path = Path.Combine(drive, candPath );
           break;
         }        
       }
       if(found) 
          break;
    }
    if(!found)
    {
        Console.Writeline("Please enter a path");
        path = Console.ReadLine();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多