我正在尝试创建一个需要查找特定文件夹的程序,通常它是“C:\ Program Files(x86)\ Steam \ SteamApps \ common \ Team Fortress 2”,但它可能是安装在不同的驱动器上或单独安装在“程序文件”中。有没有办法检查每个可用的驱动器来找到它,如果它根本找不到它(例如,E:\ Games \ Team Fortress)将布尔值或某物(boolCanFindFolder)标记为false,以便我可以提示用户输入自己的目录,然后检查它以确定它是否存在?
答案 0 :(得分:0)
要获取机器上可用驱动器的列表,
您可以使用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();
}