C#中獲取指定路徑下特定開頭和后綴的所有文件


場景

指定一個文件路徑,獲取當前路徑下所有文件,並篩選出以指定內容開頭和結尾的文件。

注:

博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。

實現

首先指定前綴和后綴名變量。

string prefix = "TestInfo_";        //實驗信息配置文件前綴
string ext = ".xml";

 

然后獲取特定路徑下的所有文件並遍歷,依次判斷是否含有前綴和后綴。

string directoryPath = Path.GetDirectoryName(node.Id); //獲取指定路徑
//存取所有文件路徑
List<string> resultList = new List<string>();
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(directoryPath);
System.IO.FileInfo[] files = di.GetFiles();
foreach (System.IO.FileInfo fi in files)
  {
        //有配置文件
       if (fi.Name.Contains(prefix) && fi.Extension.ToLower() == ext)
           {
                resultList.Add(fi.FullName);
            }
   }
//如果配置文件信息正常(只有一個配置文件)
 if (resultList != null && resultList.Count == 1)
{
    string xmlPath = resultList[0];
    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM