/// <summary>/// 递归获取文件夹目录下文件/// </summary>/// <param name="pathName">需要递归遍历的文件夹</param>/// <param name="fileRule">遍历规则『委托』</param>publicstaticvoidLoopFolder(stringpathName, Action<FileInfo> fileRule)
{if(string.IsNullOrEmpty(pathName))thrownewArgumentNullException(pathName);
Queue<string> _pathQueue =newQueue<string>();
_pathQueue.Enqueue(pathName);while(_pathQueue.Count > 0)
{string_path = _pathQueue.Dequeue();
DirectorySecurity _pathSecurity =newDirectorySecurity(_path,accessControlSections.Access);if(!_pathSecurity.AreAccessRulesPRotected)//文件夹权限是否可访问{
DirectoryInfo _directoryInfo =newDirectoryInfo(_path);foreach(DirectoryInfo diChildin_directoryInfo.GetDirectories())
{
_pathQueue.Enqueue(diChild.FullName);
}foreach(FileInfo filein_directoryInfo.GetFiles())
{
fileRule(file);
}
}
}
}
举例使用
CSharpToolV2.LoopFolder(@"C:\Users\Administrator\Downloads\", (FileInfo file) =>
{
if (file.Extension.Equals(".xls"))//获取Excel类型文件
{
Console.WriteLine(string.Format("============{0}==============", file.FullName));
}
});
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp{ background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }代码效果