日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 输出目录结构树到Console或文本文件

發布時間:2025/3/15 C# 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 输出目录结构树到Console或文本文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

1.關于打印目錄樹

前幾天寫文檔,要解釋一個目錄里大部分的子目錄和文件的用途,于是順手寫了一個打印文件目錄樹的C#工具類,可以將生成的目錄樹打印到Console或是文本文件中。

2.工具類源碼

打印目錄樹工具類:DocTreeHelper

需要手動加載命名空間:System.IO

class DocTreeHelper {/// <summary>/// 輸出目錄結構樹/// </summary>/// <param name="dirpath">被檢查目錄</param>public static void PrintTree(string dirpath){if (!Directory.Exists(dirpath)){throw new Exception("文件夾不存在");}PrintDirectory(dirpath, 0, ""); }/// <summary>/// 將目錄結構樹輸出到指定文件/// </summary>/// <param name="dirpath">被檢查目錄</param>/// <param name="outputpath">輸出到的文件</param>public static void PrintTree(string dirpath, string outputpath){if (!Directory.Exists(dirpath)){throw new Exception("文件夾不存在");}//將輸出流定向到文件 outputpathStringWriter swOutput = new StringWriter(); Console.SetOut(swOutput); PrintDirectory(dirpath, 0, ""); //將輸出流輸出到文件 outputpathFile.WriteAllText(outputpath, swOutput.ToString()); //將輸出流重新定位回文件 outputpathStreamWriter swConsole = new StreamWriter(Console.OpenStandardOutput(), Console.OutputEncoding);swConsole.AutoFlush = true;Console.SetOut(swConsole);}/// <summary>/// 打印目錄結構/// </summary>/// <param name="dirpath">目錄</param>/// <param name="depth">深度</param>/// <param name="prefix">前綴</param>private static void PrintDirectory(string dirpath, int depth, string prefix){DirectoryInfo dif = new DirectoryInfo(dirpath);//打印當前目錄if (depth == 0){Console.WriteLine(prefix + dif.Name);}else{Console.WriteLine(prefix.Substring(0, prefix.Length - 2) + "| ");Console.WriteLine(prefix.Substring(0, prefix.Length - 2) + "|-" + dif.Name);}//打印目錄下的目錄信息for (int counter = 0; counter < dif.GetDirectories().Length; counter++){DirectoryInfo di = dif.GetDirectories()[counter]; if (counter != dif.GetDirectories().Length - 1 ||dif.GetFiles().Length != 0){PrintDirectory(di.FullName, depth + 1, prefix + "| ");}else{PrintDirectory(di.FullName, depth + 1, prefix + " ");}}//打印目錄下的文件信息for (int counter = 0; counter < dif.GetFiles().Length; counter++){FileInfo f = dif.GetFiles()[counter];if (counter == 0){Console.WriteLine(prefix + "|");}Console.WriteLine(prefix + "|-" + f.Name);}} }

3.調用實例

在Main函數中輸入下面的代碼進行調用

class Program {static void Main(string[] args){string dirpath = @"D:\MyPrograms\Program4Use\DocumentTree";string outputpath = @"output.txt";DocTreeHelper.PrintTree(dirpath);DocTreeHelper.PrintTree(dirpath, outputpath);Console.WriteLine("Output Finished");Console.WriteLine("輸出完畢");Console.ReadLine();} }

4.運行效果

1)調用DocTreeHelper.PrintTree(dirpath)將目錄dirpath的信息輸出到控制臺

2)調用DocTreeHelper.PrintTree(dirpath, outputpath)將目錄dirpath的信息輸出到文本文件outputpath

5.其他注意事項

1)在輸出到文件的函數中,本文中的代碼是先將Console的輸出定位到一個流中,再在完成功能后將Console的輸出定位回控制臺來實現的。輸出到控制臺的流,編碼方式應該采用Console.OutputEncoding,如果使用Encoding.ASCII,可以正確輸出英文,但之后輸出漢字會出現亂碼。

2)不要在輸出到文本文件的內容中使用字符'\b',BackSpace在文本文件中也是一個字符,它并不會將光標向前移動。

END

轉載于:https://my.oschina.net/Tsybius2014/blog/354421

總結

以上是生活随笔為你收集整理的C# 输出目录结构树到Console或文本文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。