---
title: "c# Dos Tree "
author: "hcmfys"
date: "2008-09-15T18:31:00Z"
tags: ["C", "C#", "C++", "DOS", "J#"]
slug: "c-dos-tree"
draft: false
lastmod: "2008-09-15T18:31:00Z"
id: 588846
url: "https://hcmfys.iteye.com/blog/588846"
---

用法DOS 命令 发现 Tree 命令很有趣 就用C# 写了一个

写得不好请指教  

[http://files.cnblogs.com/hcmfys/code.rar](http://files.cnblogs.com/hcmfys/code.rar) 

![](http://images.cnblogs.com/cnblogs_com/hcmfys/test.JPG)

using System;  
using System.Collections.Generic;  
using System.Text;  
using System.IO;

namespace com.qiuSmile  
{  
    public class FileTree  
    {  
        /// &lt;summary&gt;  
        /// 此程序是模仿DOS tree功能  
        /// 作者 相思雨 hcmfys@163.com  
        /// 2008-09-14 中秋节  
        // 使用方法  
        //  FileTree.showTree("c:\\\\");  
        //如果要显示文件  FileTree.showFile=true;  
        // 如果要写入文件则加上  FileTree.writeFile("d:\\\\test.txt");  
        /// our knowlege belong to all human  
        /// &lt;/summary&gt;  
        private static int nodeCount = 1;  
        private static bool first = false;  
        private static DirectoryInfo root = null;  
        private static string splitString = "├─";  
        private static string empty = "  ";  
        private static string insertString = "│";  
        private static string fileStr = "";  
        public static bool showFile = false;// show file name  
        public static void showTree(string dir)  
        {  
            try  
            {  
                string str = "";  
                StringBuilder strBulider = new StringBuilder();

                DirectoryInfo dirDirctory = new DirectoryInfo(dir);

                if (!first)  
                {  
                    root = new DirectoryInfo(dir);  
                    first = true;  
                }  
                DirectoryInfo\[] sonDir = dirDirctory.GetDirectories();  
                int len = sonDir.Length;  
                for (int i = 0; i &lt;= len - 1; i++)  
                {  
                    str = "";

                    nodeCount = getFatherCount(sonDir\[i], root);  
                    for (int k = 0; k &lt; nodeCount; k++)  
                    {  
                        str += empty;  
                    }  
                    str += str + splitString + sonDir\[i].Name;

                    if (!sonDir\[i].Parent.FullName.Equals(root.FullName))  
                    {  
                        if (nodeCount != 0)  
                        {  
                            string tmpStr = "";  
                            for (int j = 0; j &lt; nodeCount; j++)  
                            {  
                                int index = -1;  
                                tmpStr += empty;  
                                index = tmpStr.Length;  
                                if (j == 0) index = 0;  
                                if (j &gt; 1) index = index + (j - 1) * 2;  
                                str = str.Insert(index, insertString);  
                                str = str.Remove(index + insertString.Length, insertString.Length);  
                            }  
                        }  
                    }  
                    Console.WriteLine(str);  
                    fileStr += str + "\\n";  
                    showTree(sonDir\[i].FullName);  
                }  
                if (showFile)  
                {  
                    FileInfo\[] fSonDir = dirDirctory.GetFiles();  
                    len = fSonDir.Length;  
                    for (int i = 0; i &lt;= len - 1; i++)  
                    {  
                        str = "";

                        nodeCount = getFatherCount(fSonDir\[i].Directory, root);  
                        for (int k = 0; k &lt; nodeCount; k++)  
                        {  
                            str += empty;  
                        }  
                        str += str + splitString +"&gt; "+ fSonDir\[i].Name;

                        if (!fSonDir\[i].Directory.FullName.Equals(root.FullName))  
                        {  
                            if (nodeCount != 0)  
                            {  
                                string tmpStr = "";  
                                for (int j = 0; j &lt; nodeCount; j++)  
                                {  
                                    int index = -1;  
                                    tmpStr += empty;  
                                    index = tmpStr.Length;  
                                    if (j == 0) index = 0;  
                                    if (j &gt; 1) index = index + (j - 1) * 2;  
                                    str = str.Insert(index, insertString);  
                                    str = str.Remove(index + insertString.Length, insertString.Length);  
                                }  
                            }  
                        }

                        Console.WriteLine(str);  
                        fileStr += str + "\\n";  
                    }

                }  
            }  
            catch (Exception) { }  
        }  
        /// &lt;summary&gt;  
        /// 获取父节点的数目  
        /// &lt;/summary&gt;  
        /// &lt;param name="myInfo"&gt;&lt;/param&gt;  
        /// &lt;param name="root"&gt;&lt;/param&gt;  
        /// &lt;returns&gt;&lt;/returns&gt;  
        private static int getFatherCount(DirectoryInfo myInfo, DirectoryInfo root)  
        {  
            int i = 0;  
            DirectoryInfo father = myInfo.Parent;  
            while (father != null && !father.FullName.Equals(root.FullName))  
            {  
                father = father.Parent;  
                i++;  
            }  
            return i;  
        }

        public static void writeFile(string fileName)  
        {  
            File.WriteAllText(fileName, fileStr);  
        }  
        /// &lt;summary&gt;  
        /// test main method  
        /// &lt;/summary&gt;  
        public static void test()  
        {  
            Console.ForegroundColor = ConsoleColor.White;  
            Console.BackgroundColor = ConsoleColor.Black;  
            Console.Title = "c# dos Tree";  
            Console.WriteLine("请你输入要显示的目录");  
            string inputString = Console.ReadLine();  
            //FileTree.showFile = true;  
            if (inputString.Trim().Length &gt; 0)  
            {  
                showTree(inputString);  
            }  
            writeFile("d:\\\\12.txt");  
            Console.WriteLine("按任意键退出.....");  
            Console.Read();  
        }

    }

}