简体   繁体   English

如何使用C#在多个目录中找到文件

[英]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);
}
  1. You need to split your string from comma: 您需要从逗号分割字符串:

string paths= 'C:/hello,D:/Hello'; 字符串路径='C:/ hello,D:/ Hello';

 string[] words = paths.Split(',');
  1. 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.

相关问题 SevenZipSharp-如何使用C#将多个目录压缩为单个文件? - SevenZipSharp - how to compress multiple directories into a single file using c#? 在C#中使用GetFiles()时如何定位相对文件路径? - How to locate relative file path when using GetFiles() in C#? 如何使用c#获取包含特定文件的目录列表 - How to get list of directories containing particular file using c# 如何使用C#在Pandoc中更改目录? - How changing directories in Pandoc using C#? 如何使用C#切换目录? - How to switch directories using C#? 如何使用 Webdriver 和 C# 通过 Selenium 定位并单击嵌套在多个框架和框架集中的元素 - How to locate and click on an element which is nested within multiple frame and frameset through Selenium using Webdriver and C# C#-File Watcher作为Windows服务监视多个目录 - C# - File Watcher as a windows service watching multiple directories C#在目录中查找文件 - c# finding file in directories 如何使用C#以编程方式打开服务器上的Office文件? - How to programmatically open an Office file locate on a Server with c#? 如何在C#中从单个完整路径创建多个目录? - How to create multiple directories from a single full path in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM