如何從目錄中獲取文件特定的文件名?

[英]How can i get files specific files names from directory?


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)會起作用


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/01/14/f4f6c92cf5dcc9c3afb397e1a5e2c33b.html



 
  © 2014-2022 ITdaan.com 联系我们: