c# Dos Tree
用法DOS 命令 发现 Tree 命令很有趣 就用C# 写了一个
写得不好请指教
https://files.cnblogs.com/hcmfys/code.rar
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace com.qiuSmile
{
public class FileTree
{
/// <summary>
/// 此程序是模仿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
/// </summary>
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 <= len - 1; i++)
{
str = "";
nodeCount = getFatherCount(sonDir[i], root);
for (int k = 0; k < 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 < nodeCount; j++)
{
int index = -1;
tmpStr += empty;
index = tmpStr.Length;
if (j == 0) index = 0;
if (j > 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 <= len - 1; i++)
{
str = "";
nodeCount = getFatherCount(fSonDir[i].Directory, root);
for (int k = 0; k < nodeCount; k++)
{
str += empty;
}
str += str + splitString +"> "+ fSonDir[i].Name;
if (!fSonDir[i].Directory.FullName.Equals(root.FullName))
{
if (nodeCount != 0)
{
string tmpStr = "";
for (int j = 0; j < nodeCount; j++)
{
int index = -1;
tmpStr += empty;
index = tmpStr.Length;
if (j == 0) index = 0;
if (j > 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) { }
}
/// <summary>
/// 获取父节点的数目
/// </summary>
/// <param name="myInfo"></param>
/// <param name="root"></param>
/// <returns></returns>
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);
}
/// <summary>
/// test main method
/// </summary>
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 > 0)
{
showTree(inputString);
}
writeFile("d:\\12.txt");
Console.WriteLine("按任意键退出.....");
Console.Read();
}
}
}