C#遍歷文件夾及其子目錄的完整實(shí)現(xiàn)方法
本文實(shí)例講述了C#遍歷文件夾及其子目錄的完整實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Security.AccessControl; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("STT"); string str = @"E:\"; if (!str.EndsWith("\\")) { str += "\\"; } IList<FileInfo> lst = GetFiles(str); if (!Directory.Exists(str)) { try { Directory.CreateDirectory(str); } catch(Exception e) { Console.WriteLine(e.Message); Console.ReadKey(); return; } } if (File.Exists(str + "test.txt")) { File.Delete(str + "test.txt"); } FileInfo file = new FileInfo(str + "test.txt"); if (!file.Directory.Exists) { Directory.CreateDirectory(file.DirectoryName); } using (StreamWriter outFileWriter = new StreamWriter(str + "test.txt", false, Encoding.UTF8)) { StringBuilder sb = new StringBuilder(); foreach (FileInfo item in lst) { sb.Append("\""); sb.Append(item.FullName); sb.Append("\""); sb.Append(","); sb.Append("\r\n"); } sb.Remove(sb.Length - 2, 2); outFileWriter.WriteLine(sb.ToString()); outFileWriter.Flush(); outFileWriter.Close(); } Console.WriteLine("END"); Console.ReadKey(); } private static void GetDirectorys(string strPath, ref List<string> lstDirect) { DirectoryInfo diFliles = new DirectoryInfo(strPath); DirectoryInfo[] diArr = diFliles.GetDirectories(); //DirectorySecurity directorySecurity = null; foreach (DirectoryInfo di in diArr) { try { //directorySecurity = new DirectorySecurity(di.FullName, AccessControlSections.Access); //if (!directorySecurity.AreAccessRulesProtected) //{ lstDirect.Add(di.FullName); GetDirectorys(di.FullName, ref lstDirect); //} } catch { continue; } } } /// <summary> /// 遍歷當(dāng)前目錄及子目錄 /// </summary> /// <param name="strPath">文件路徑</param> /// <returns>所有文件</returns> private static IList<FileInfo> GetFiles(string strPath) { List<FileInfo> lstFiles = new List<FileInfo>(); List<string> lstDirect = new List<string>(); lstDirect.Add(strPath); DirectoryInfo diFliles = null; GetDirectorys(strPath, ref lstDirect); foreach (string str in lstDirect) { try { diFliles = new DirectoryInfo(str); lstFiles.AddRange(diFliles.GetFiles()); } catch { continue; } } return lstFiles; } } }
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#遍歷算法與技巧總結(jié)》、《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》、《C#操作Excel技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見(jiàn)控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
上一篇:c#通用登錄模塊分享
欄 目:C#教程
下一篇:C#實(shí)現(xiàn)順序表(線性表)完整實(shí)例
本文標(biāo)題:C#遍歷文件夾及其子目錄的完整實(shí)現(xiàn)方法
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6390.html
您可能感興趣的文章
- 01-10C#刪除只讀文件或文件夾(解決File.Delete無(wú)法刪除文件)
- 01-10C#遍歷文件夾后上傳文件夾中所有文件錯(cuò)誤案例分析
- 01-10C#編程實(shí)現(xiàn)獲取文件夾中所有文件的文件名
- 01-10C#中用foreach語(yǔ)句遍歷數(shù)組及將數(shù)組作為參數(shù)的用法
- 01-10C#程序中創(chuàng)建、復(fù)制、移動(dòng)、刪除文件或文件夾的示例
- 01-10Windows中使用C#為文件夾和文件編寫密碼鎖的示例分享
- 01-10在C#中g(shù)lobal關(guān)鍵字的作用及其用法
- 01-10C#如何遍歷Dictionary
- 01-10詳解C#中的定時(shí)器Timer類及其垃圾回收機(jī)制
- 01-10C#中哈希表(HashTable)用法實(shí)例詳解(添加/移除/判斷/遍歷/排序等


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery