I'm trying to make a program that needs to find a specific folder to work, generally it would be "C:\Program Files (x86)\Steam\SteamApps\common\Team Fortress 2", but it could be installed on different drives or in "Program Files" alone. Is there any way to check every drive available to find this, and if it can't find it at all (for instance, E:\Games\Team Fortress) mark a boolean or something (boolCanFindFolder) as false so that I could prompt the user to enter their own directory, and then check it to be sure it exists?
Asked
Active
Viewed 1,158 times
-1
-
1You're looking for `DriveInfo.GetDrives()` and `Directory.Exists()`. – SLaks Mar 09 '16 at 02:38
-
So perhaps I could get all the drives and check each one individually? Could you maybe give a little rough outline on how I would go about setting that up? Cheers! – Max Box Mar 09 '16 at 02:42
-
2@MaxBox - We're not a code writing service. In this case Google is your friend. You should be able to find a ton of references how to do this. When you do try to implement it and you get stuck then come back here and ask us a question showing your code and we'll be glad to help. That's what Stack Overflow is about - helping with **specific** programming problems. – Enigmativity Mar 09 '16 at 02:46
-
You're looking for a loop, or LINQ with `FirstOrDefault()`. – SLaks Mar 09 '16 at 02:47
-
2@MaxBox Use `drives = DriveInfo.GetDrives()`. Setup a `foreach(var drive in drives)`. Then loop of your guesses (for example `Program Files (x86)\Steam\SteamApps\common\Team Fortress 2`) - `foreach (var expectedPath in expectedPaths)` and do something like `Directory.Exists(Path.Combine(drive, expectedPath))` – Rob Mar 09 '16 at 02:47
-
This should work perfectly, thank you very much!! – Max Box Mar 09 '16 at 02:49
-
To find an installation folder for a specific product, a more reliable approach would probably be to search by other means - like registry or (in TF2's case) [Steam registry](http://stackoverflow.com/questions/33662175/retrieve-a-list-of-installed-games-from-the-steam-api). – ivan_pozdeev Mar 09 '16 at 05:54
1 Answers
0
To get a list of the drives available on your machine,
you can use the System.IO.DriveInfo class.
Here is an example to solve your problem:
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();
}

Tal Avissar
- 10,088
- 6
- 45
- 70