我的路径是
E:\ Program Files(x86)\ foobar2000 \ library \
我需要找到一个文件,其文件名包含变量lastline。
然后我需要将filepath + filename保存为字符串。
我试过......
string[] filePaths = Directory.GetFiles("E:\\Program Files (x86)\\foobar2000\\library\\", " *.*", SearchOption.AllDirectories);
for (int i = 0; i < filePaths.Length; i++) {
if (filePaths[i].Contains(lastline)) {
Console.WriteLine(filePaths[i]);
}
}
它所做的就是返回每个文件,而不是包含字符串lastline的文件
答案 0 :(得分:-1)
try
{
string lastline = "Controller"; // assuming you know the file your searching for
foreach (string filePaths in Directory.GetDirectories(@"E:\Program Files (x86)\foobar2000\library\"))
{
foreach (string f in Directory.GetFiles(filePaths, "*" + lastline + "*.*"))
{
Console.WriteLine(f); // would print the filepath n the file name
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
希望这有帮助。