文件的操作

Directory

1.                List<string> files=  Directory.GetFiles(fpath).Where(file=>file.EndsWith(".txt")).ToList();
           string fpath = folderBrowserDialog1.SelectedPath;
                string[] files1 = Directory.GetFiles(fpath);//Directory.GetFiles(fpath,"*.txt");
2.

var filteredFiles = Directory
 .GetFiles(path, "*.*")
 .Where(file => file.ToLower().EndsWith("aspx") || file.ToLower().EndsWith("ascx"))
 .ToList();


3. GetFiles的只能匹配单个模式,但你LINQ调用GetFiles的多模式:

FileInfo[] fi = new string[]{"*.txt","*.doc"}
 .SelectMany(i => di.GetFiles(i, SearchOption.AllDirectories))
 .Distinct().ToArray();

这里请参阅:
4. 我怕你会做财产以后这样,我从这里突变的正则表达式。

var searchPattern = new Regex(
 @"$(?<=\.(aspx|ascx))", 
 RegexOptions.IgnoreCase);
var files = Directory.GetFiles(path).Where(f => searchPattern.IsMatch(f));


5. 我会尝试像指定

var searchPattern = "as?x";

它应该工作。
6. 我下面的:

var ext = new string[] { ".ASPX", ".ASCX" };
FileInfo[] collection = (from fi in new DirectoryInfo(path).GetFiles()
       where ext.Contains(fi.Extension.ToUpper())
       select fi)
       .ToArray();

编辑:纠正目录和DirectoryInfo之间因不匹配
7.

 /// <summary>
 /// Returns the names of files in a specified directories that match the specified patterns using LINQ
 /// </summary>
 /// <param name="srcDirs">The directories to seach</param>
 /// <param name="searchPatterns">the list of search patterns</param>
 /// <param name="searchOption"></param>
 /// <returns>The list of files that match the specified pattern</returns>
 public static string[] GetFilesUsingLINQ(string[] srcDirs,
   string[] searchPatterns,
   SearchOption searchOption = SearchOption.AllDirectories)
 {
  var r = from dir in srcDirs
    from searchPattern in searchPatterns
    from f in Directory.GetFiles(dir, searchPattern, searchOption)
    select f;
  return r.ToArray();
 }


8.

 public static bool CheckFiles(string pathA, string pathB)
 {
  string[] extantionFormat = new string[] { ".war", ".pkg" };
  return CheckFiles(pathA, pathB, extantionFormat);
 }
 public static bool CheckFiles(string pathA, string pathB, string[] extantionFormat)
 {
  System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(pathA);
  System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(pathB);
  // Take a snapshot of the file system. list1/2 will contain only WAR or PKG 
  // files
  // fileInfosA will contain all of files under path directories 
  FileInfo[] fileInfosA = dir1.GetFiles("*.*", 
        System.IO.SearchOption.AllDirectories);
  // list will contain all of files that have ..extantion[] 
  // Run on all extantion in extantion array and compare them by lower case to 
  // the file item extantion ...
  List<System.IO.FileInfo> list1 = (from extItem in extantionFormat
           from fileItem in fileInfosA
           where extItem.ToLower().Equals 
           (fileItem.Extension.ToLower())
           select fileItem).ToList();
  // Take a snapshot of the file system. list1/2 will contain only WAR or 
  // PKG files
  // fileInfosA will contain all of files under path directories 
  FileInfo[] fileInfosB = dir2.GetFiles("*.*", 
          System.IO.SearchOption.AllDirectories);
  // list will contain all of files that have ..extantion[] 
  // Run on all extantion in extantion array and compare them by lower case to 
  // the file item extantion ...
  List<System.IO.FileInfo> list2 = (from extItem in extantionFormat
           from fileItem in fileInfosB
           where extItem.ToLower().Equals   
           (fileItem.Extension.ToLower())
           select fileItem).ToList();
  FileCompare myFileCompare = new FileCompare();
  // This query determines whether the two folders contain 
  // identical file lists, based on the custom file comparer 
  // that is defined in the FileCompare class. 
  return list1.SequenceEqual(list2, myFileCompare);
 }


9.

var filteredFiles = Directory
 .EnumerateFiles(path, "*.*") // .NET4 better than `GetFiles`
 .Where(
  // ignorecase faster than tolower...
  file => file.ToLower().EndsWith("aspx")
  || file.EndsWith("ascx", StringComparison.OrdinalIgnoreCase))
 .ToList();

不要忘了新的.NET4Directory.EnumerateFiles对于性能提升(什么是目录之间的差异。与Directory.GetFiles?) “IGNORECASE”应该比“TOLOWER”快 或者,它可能会更快splitting的glob(至少它看起来更干净):

"*.ext1;*.ext2".Split(';')
 .SelectMany(g => Directory.EnumerateFiles(path, g))
 .ToList();


10. 像这样的演示:

void Main()
{
 foreach(var f in GetFilesToProcess("c:\\", new[] {".xml", ".txt"}))
  Debug.WriteLine(f);
}
private static IEnumerable<string> GetFilesToProcess(string path, IEnumerable<string> extensions)
{
 return Directory.GetFiles(path, "*.*")
  .Where(f => extensions.Contains(Path.GetExtension(f).ToLower()));
}

 

转载于:https://www.cnblogs.com/nanxiaoxiang/p/5148939.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值