如何从目录中获取文件特定的文件名?

时间:2022-02-16 07:08:27
string[] list = Directory.GetFiles(countriesMainPath + "\\" + currentDownloadCountry, "*.jpg");

But not only gif i want to specify that also it will be only files that contains the name infrared. For example i have files name 0infrared.gif and also 0visible.gif and i want to get all the files that are infrared.

但不仅gif我想指定它也只是包含名称红外的文件。例如,我有文件名称0infrared.gif和0visible.gif,我想得到所有红外文件。

5 个解决方案

#1


0  

 string partialName = "infrared";
 DirectoryInfo dir = new DirectoryInfo(@"c:\");
 FileInfo[] filesInDir = dir.GetFiles("*" + partialName + "*.*");
 foreach (FileInfo foundFile in filesInDir)
 {
    string fullName = foundFile.FullName;
    Console.WriteLine(fullName);
 }    

#2


1  

This will get you all files with infrared in file name and with extension jpg

这将为您提供所有文件名为红外且扩展名为jpg的文件

string[] list = Directory.GetFiles(countriesMainPath + "\\" + currentDownloadCountry, 
             "*infrared*.jpg");

#3


1  

string[] list = Directory.GetFiles(
    countriesMainPath + "\\" + currentDownloadCountry, 
    "*infrared*.*");

This will return all the files contain infrared in their file name.

这将返回其文件名中包含红外线的所有文件。

#4


1  

I think this may be what you look for. ref: MSDN

我想这可能就是你要找的东西。 ref:MSDN

public static string[] GetFiles(string path, string searchPattern, SearchOption searchOption)
{
    string[] searchPatterns = searchPattern.Split('|');
    List<string> files = new List<string>();
    foreach (string sp in searchPatterns)
        files.AddRange(System.IO.Directory.GetFiles(path, sp, searchOption));
    files.Sort();
    return files.ToArray();
}

Usage

用法

var wantedImgs = GetFiles(
    dirYouWant, "*infrared*.jpg|*infrared*.gif", 
    earchOption.TopDirectoryOnly);

#5


-1  

maybe *infrared*.(jpg|gif) will work

也许*红外*。(jpg | gif)会起作用

#1


0  

 string partialName = "infrared";
 DirectoryInfo dir = new DirectoryInfo(@"c:\");
 FileInfo[] filesInDir = dir.GetFiles("*" + partialName + "*.*");
 foreach (FileInfo foundFile in filesInDir)
 {
    string fullName = foundFile.FullName;
    Console.WriteLine(fullName);
 }    

#2


1  

This will get you all files with infrared in file name and with extension jpg

这将为您提供所有文件名为红外且扩展名为jpg的文件

string[] list = Directory.GetFiles(countriesMainPath + "\\" + currentDownloadCountry, 
             "*infrared*.jpg");

#3


1  

string[] list = Directory.GetFiles(
    countriesMainPath + "\\" + currentDownloadCountry, 
    "*infrared*.*");

This will return all the files contain infrared in their file name.

这将返回其文件名中包含红外线的所有文件。

#4


1  

I think this may be what you look for. ref: MSDN

我想这可能就是你要找的东西。 ref:MSDN

public static string[] GetFiles(string path, string searchPattern, SearchOption searchOption)
{
    string[] searchPatterns = searchPattern.Split('|');
    List<string> files = new List<string>();
    foreach (string sp in searchPatterns)
        files.AddRange(System.IO.Directory.GetFiles(path, sp, searchOption));
    files.Sort();
    return files.ToArray();
}

Usage

用法

var wantedImgs = GetFiles(
    dirYouWant, "*infrared*.jpg|*infrared*.gif", 
    earchOption.TopDirectoryOnly);

#5


-1  

maybe *infrared*.(jpg|gif) will work

也许*红外*。(jpg | gif)会起作用