[英]c# Search and get path from Filename
我想在已連接的現有邏輯驅動器中查找文件,但是當我這樣做時,我最終得到的是string [],我真的不知道該如何處理...所以我想做的是在這里,搜索通常具有FAT32或NTFS格式的“硬盤”驅動器...(請告訴我是否經常使用其他任何驅動器)然后,我得到該驅動器的“字母”,並嘗試從那里搜索csgo.exe文件。 您可能可以找出其余的...
這是我的代碼...
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!");
}
}
}
您的代碼是正確的,您將以string []結尾,因為這是您在以下行中聲明的變量:
String[] csgofile = Directory.GetFiles(StartDir, "test.txt", SearchOption.TopDirectoryOnly);
如果該文件不存在,並且您對其進行了調試,它將看起來像
csgofile|{string[0]}
如果成功獲取文件,它將是:
csgofile|{string[1]}
請注意,您只是在TopDirectoryOnly上搜索文件,因此請確保該文件確實存在於要搜索的驅動器中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.