我需要检查在给定的子文件夹集中是否存在某个文件(.config文件),但我不知道这些子文件夹的名称是什么(因为该子文件夹由用户首选项命名)这些子文件夹将仅位于给定目录内。
如何将该子文件夹的名称获取到可以使用File.Exists的路径,以检查文件是否存在。
if (File.Exists(@ "D:\TEST\PROJ\Repo\{lastFolderName}\fileserver.config")) {
MessageBox.Show("File Found");
} else {
MessageBox.Show("File Not Found");
}
答案 0 :(得分:0)
您必须尝试这样
string path =@ "D:\TEST\PROJ\Repo\";
DirectoryInfo directory = new DirectoryInfo(path);
DirectoryInfo[] subDirectories = directory.GetDirectories();
foreach(DirectoryInfo folder in subDirectories)
{
string subpath = Path.Combine( @ "D:\TEST\PROJ\Repo\", folder.Name);
string[] filePaths = Directory.GetFiles(subpath, "fileserver.config");
if(filePaths.Any())
Console.WriteLine(subpath);
}