简体   繁体   English

如何通过部分名称查找文件?

[英]How to find the file by its partial name?

How can I get the full filename?我怎样才能得到完整的文件名?

For example:例如:

I have a file named 171_s.jpg that is stored on the hard disc.我有一个名为171_s.jpg的文件存储在硬盘上。

I need to find the file by its partial name, ie 171_s , and get the full name.我需要通过文件的部分名称(即171_s )找到该文件,并获取全名。

How can I implement this?我该如何实施?

Here's an example using GetFiles():下面是一个使用 GetFiles() 的例子:

static void Main(string[] args)
{
    string partialName = "171_s";
    DirectoryInfo hdDirectoryInWhichToSearch = new DirectoryInfo(@"c:\");
    FileInfo[] filesInDir = hdDirectoryInWhichToSearch.GetFiles("*" + partialName + "*.*");

    foreach (FileInfo foundFile in filesInDir)
    {
        string fullName = foundFile.FullName;
        Console.WriteLine(fullName);
    }    
}

Update - Jakub answer is more efficient way to do.更新 - Jakub 回答是更有效的方法。 ie, use System.IO.Directory.GetFiles() http://msdn.microsoft.com/en-us/library/ms143316.aspx即,使用 System.IO.Directory.GetFiles() http://msdn.microsoft.com/en-us/library/ms143316.aspx

The answer has been already posted, however for an easy understanding here is the code答案已经发布,但为了便于理解,这里是代码

string folderPath = @"C:/Temp/";
DirectoryInfo dir= new DirectoryInfo(folderPath);
FileInfo[] files = dir.GetFiles("171_s*", SearchOption.TopDirectoryOnly);
foreach (var item in files)
{
    // do something here
}

You could use System.IO.Directory.GetFiles()你可以使用System.IO.Directory.GetFiles()

http://msdn.microsoft.com/en-us/library/ms143316.aspx http://msdn.microsoft.com/en-us/library/ms143316.aspx

public static string[] GetFiles(
    string path,
    string searchPattern,
    SearchOption searchOption
)

path Type: System.String The directory to search. path类型:System.String 要搜索的目录。

searchPattern Type: System.String The search string to match against the names of files in path. searchPattern类型:System.String 与路径中文件名匹配的搜索字符串。 The parameter cannot end in two periods ("..") or contain two periods ("..") followed by DirectorySeparatorChar or AltDirectorySeparatorChar, nor can it contain any of the characters in InvalidPathChars.该参数不能以两个句点 ("..") 结尾或包含两个句点 ("..") 后跟 DirectorySeparatorChar 或 AltDirectorySeparatorChar,也不能包含 InvalidPathChars 中的任何字符。

searchOption Type: System.IO.SearchOption One of the SearchOption values that specifies whether the search operation should include all subdirectories or only the current directory. searchOption类型:System.IO.SearchOption SearchOption 值之一,用于指定搜索操作应包括所有子目录还是仅包括当前目录。

You can do it like this:你可以这样做:

....

List<string> _filesNames;

foreach(var file in _directory)
{
    string name = GetFileName(file);
    if(name.IndexOf(_partialFileName) > 0)
    {
      _fileNames.Add(name);   
    }
}
....

Simple as that:就那么简单:

string path = @"C:\example\directory";
string searchPattern = "*171_s*";
string[] filePaths = Directory.GetFiles(path, searchPattern,SearchOption.AllDirectories);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM