C# 輸出目錄結構樹到Console或文本文件

1.關於打印目錄樹函數

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

2.工具類源碼編碼

打印目錄樹工具類:DocTreeHelpercode

須要手動加載命名空間: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("文件夾不存在");
        }

        //將輸出流定向到文件 outputpath
        StringWriter swOutput = new StringWriter(); 
        Console.SetOut(swOutput); 

        PrintDirectory(dirpath, 0, ""); 

        //將輸出流輸出到文件 outputpath
        File.WriteAllText(outputpath, swOutput.ToString()); 

        //將輸出流從新定位迴文件 outputpath
        StreamWriter 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函數中輸入下面的代碼進行調用string

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.運行效果it

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

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

5.其餘注意事項

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

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

END

相關文章
相關標籤/搜索