[英]How to locate a file on multiple directories using C#
I am searching for a file say "abc.txt" on multiple directories. 我正在多个目录中搜索“ abc.txt”文件。 These directories are comma seperated values like 这些目录是逗号分隔的值,例如
string paths= 'C:/hello,D:/Hello';
How can I search for "abc.txt" using the above comma seperated directories? 如何使用上述逗号分隔的目录搜索“ abc.txt”?
Thanks. 谢谢。
You will just need to split the string on the commas then use the DirectoryInfo
class to search each directory in turn: 您只需要在逗号上分割字符串,然后使用DirectoryInfo
类依次搜索每个目录:
http://msdn.microsoft.com/en-us/library/ms143327.aspx http://msdn.microsoft.com/en-us/library/ms143327.aspx
string paths= 'C:/hello,D:/Hello';
string[] pathList = paths.Split(',');
string searchPattern = "abc.txt";
foreach (string path in pathList)
{
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] files = di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly);
}
string paths= 'C:/hello,D:/Hello'; 字符串路径='C:/ hello,D:/ Hello';
string[] words = paths.Split(',');
Now you need to get the directory letter from each string token 现在您需要从每个字符串标记中获取目录字母
foreach (string word in words) { foreach(字串字){
string directoryName = word.Split(':/')[0]; 字符串directoryName = word.Split(':/')[0];
string searchString = word.Split(':/')[1]; 字符串searchString = word.Split(':/')[1];
} }
Now write your search logic to search in directory. 现在,编写您的搜索逻辑以在目录中搜索。
Split your string based on comma, (I hope you don't have any comma in directory names) 根据逗号分割字符串(希望目录名中没有逗号)
string[] directories = paths.Split(',');
var files = new List<string>();
foreach (string str in directories)
{
DirectoryInfo d = new DirectoryInfo(str);
files.AddRange(Directory.GetFiles(d.FullName, "abc.txt", SearchOption.AllDirectories));
}
Your files will contain all the abc.txt files in the directories with complete path 您的文件将包含目录中具有完整路径的所有abc.txt文件
I would not suggest using a comma separated list unless you have absolute control over file names (which I assume you don't since you need to to search multiple places). 我不建议您使用逗号分隔的列表,除非您对文件名拥有绝对控制权(我假设您没有此权限,因为您需要搜索多个位置)。
Keep in mind that file names can contain characters like ',' and ';' 请记住,文件名可以包含','和';'等字符 which would be an obvious choice for separating the list. 这是分隔列表的明显选择。 If you're in control of creating the list I'd suggest using the pipe char ('|'), it's readable and it can't be part of a file name. 如果您可以控制创建列表,我建议使用管道字符('|'),它是可读的,并且不能成为文件名的一部分。
But if you have control over the file names you can simply use split like others have already suggested. 但是,如果您可以控制文件名,则可以像其他建议的那样简单地使用split。
Assuming that there are no commas in file or directory names 假设文件名或目录名中没有逗号
string paths= @"C:/hello,D:/Hello";
string multipaths = paths.Split(',');
foreach (string str in multipaths)
{
string filepath = Path.Combine(str, "abc.txt");
//Do what you want from these files.
}
your can use split method for that 您可以使用拆分方法
string paths= 'C:/hello,D:/Hello';
string[] words = paths.Split(',');
foreach (string word in words)
{
SearchInDirectory(word)
}
split Your folders string and search that directory 分割您的文件夹字符串并搜索该目录
to search for folder and files you may wanna have a look here http://msdn.microsoft.com/en-us/library/dd997370.aspx 搜索文件夹和文件,您可能想在这里看看http://msdn.microsoft.com/en-us/library/dd997370.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.