简体   繁体   中英

c# Search and get path from Filename

I want to look for a file inside the existing logical drives connected, but when I do that, i end up with an string[], which i really don't know how to handle... So what I'm trying to do here, is search in the "hard disc" drives which normally have the format FAT32 or NTFS... (please tell me if there are any others often used) And then, i get the "letter" for that drive, and tries to search for the csgo.exe file from there. And you can probably figure out the rest...

Here's my code...

if (d.DriveFormat.ToString() == "FAT32" || d.DriveFormat.ToString() == "NTFS")
{
    string StartDir = d.RootDirectory.ToString();
    String[] csgofile = Directory.GetFiles(StartDir, "csgo.exe", SearchOption.TopDirectoryOnly);
    foreach (String file in csgofile)
    {
        if (File.Exists(file))
        {
            MessageBox.Show("Drive: " + StartDir + ", CS:GO Path: " + file, "Path Found!");
        }
    }
}

Your code is correct, you will end up on string[] since that is the variable you declare on this line:

String[] csgofile = Directory.GetFiles(StartDir, "test.txt", SearchOption.TopDirectoryOnly);

If the file doesn't exist, and you debug it, it will look like

csgofile|{string[0]}

If it gets the file successfully, it will be:

csgofile|{string[1]}

Please take note that you are just searching the file on TopDirectoryOnly so make sure the file really exist in your the drive you are searching.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM