[英]SevenZipSharp - how to compress multiple directories into a single file using c#?
[英]How to locate a file on multiple directories using C#
我正在多个目录中搜索“ abc.txt”文件。 这些目录是逗号分隔的值,例如
string paths= 'C:/hello,D:/Hello';
如何使用上述逗号分隔的目录搜索“ abc.txt”?
谢谢。
您只需要在逗号上分割字符串,然后使用DirectoryInfo
类依次搜索每个目录:
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);
}
字符串路径='C:/ hello,D:/ Hello';
string[] words = paths.Split(',');
现在您需要从每个字符串标记中获取目录字母
foreach(字串字){
字符串directoryName = word.Split(':/')[0];
字符串searchString = word.Split(':/')[1];
}
现在,编写您的搜索逻辑以在目录中搜索。
根据逗号分割字符串(希望目录名中没有逗号)
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));
}
您的文件将包含目录中具有完整路径的所有abc.txt文件
我不建议您使用逗号分隔的列表,除非您对文件名拥有绝对控制权(我假设您没有此权限,因为您需要搜索多个位置)。
请记住,文件名可以包含','和';'等字符 这是分隔列表的明显选择。 如果您可以控制创建列表,我建议使用管道字符('|'),它是可读的,并且不能成为文件名的一部分。
但是,如果您可以控制文件名,则可以像其他建议的那样简单地使用split。
假设文件名或目录名中没有逗号
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.
}
您可以使用拆分方法
string paths= 'C:/hello,D:/Hello';
string[] words = paths.Split(',');
foreach (string word in words)
{
SearchInDirectory(word)
}
分割您的文件夹字符串并搜索该目录
搜索文件夹和文件,您可能想在这里看看http://msdn.microsoft.com/en-us/library/dd997370.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.