惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
K
Kaspersky official blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Security Latest
Security Latest
P
Privacy & Cybersecurity Law Blog
Google DeepMind News
Google DeepMind News
Simon Willison's Weblog
Simon Willison's Weblog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
O
OpenAI News
Help Net Security
Help Net Security
N
News and Events Feed by Topic
博客园 - 司徒正美
U
Unit 42
Security Archives - TechRepublic
Security Archives - TechRepublic
The Cloudflare Blog
D
DataBreaches.Net
Y
Y Combinator Blog
AI
AI
L
LINUX DO - 最新话题
C
CXSECURITY Database RSS Feed - CXSecurity.com
H
Heimdal Security Blog
宝玉的分享
宝玉的分享
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
TaoSecurity Blog
TaoSecurity Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Project Zero
Project Zero
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Jina AI
Jina AI
M
MIT News - Artificial intelligence
Microsoft Azure Blog
Microsoft Azure Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Attack and Defense Labs
Attack and Defense Labs
V
V2EX
博客园 - 【当耐特】
S
SegmentFault 最新的问题
小众软件
小众软件
L
LangChain Blog
N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
T
Tor Project blog
AWS News Blog
AWS News Blog
博客园 - 三生石上(FineUI控件)
Recent Announcements
Recent Announcements
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
S
Securelist
T
Troy Hunt's Blog

博客园 - 爱吃糖豆的猪

Manjaro Samba 设置共享文件夹供Windows访问 【自学Python系列】Python 基础 内部数据结构之列表和元组 【自学Python系列】Python 基础 (字符串,整数,注释) 控制台I/O显示格式化的结果 异步拷贝文件 【超详细教程】使用Windows Live Writer 2012和Office Word 2013 发布文章到博客园全面总结 C#操作符??和?: ETL 解析 简单通用的访问CVS的方法 一些概念的随笔(转) 3种思维培养有决策力的孩子 是夫妻就一起把它戒了吧! 一个10年SEO工作者的35个SEO经验 可以让你少奋斗10年的工作经验 如何在一年内拥有十年的工作经验(值得你反复读5遍以上) TreeView 递归选择父节点和子节点 文件操作 C# 中的 ConfigurationManager类引用方法 添加Word,Excel等dll时如何操作。
如何在Directory.GetFiles()方法中设置多个格式呢?
爱吃糖豆的猪 · 2015-12-03 · via 博客园 - 爱吃糖豆的猪

什么是语法设置多个文件扩展名的searchPatternDirectory.GetFiles()?例如过滤掉的文件与x和的。ascx扩展。

// TODO: Set the string 'searchPattern' to only get files with
// the extension '.aspx' and '.ascx'.
var filteredFiles = Directory.GetFiles(path, searchPattern);

LINQ是不是一种选择?它必须是一个searchPattern传入GetFiles,在这个问题中指定。

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


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

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

我怕你会做财产以后这样,我从这里突变的正则表达式。

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

我会尝试像指定

var searchPattern = "as?x";

它应该工作。 

 
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之间因不匹配 

/// <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();
 }
 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);
 }
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();

像这样的演示:

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()));
}