logo   

Thinking ...: C#打印树形目录列表.

/*很长时间没有写过东西了,无聊的时候就做些无聊的事.发现局域网里面共享的东西没有整理,放的很乱,这样可以自动生成个文件目录,这才发现自己电脑里的文件那么多..C# 版的以文件目录树的形式打印出文件列表.存储在FileList.txt文本档中.不过我觉得打印得还是不够漂亮*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
namespace ListFile
{
class Program
{
static int num = 0;
static int last = 0;
static StreamWriter sw;
static void listfile(string dir)
{
string blank = "";
int k = 0;
while (k < last)
{
blank += " ";
k++;
}
blank += "|";
while (k < num)
{
blank += "-";
k++;
}
;
sw.WriteLine(blank + dir.Substring(dir.LastIndexOf('\\') + 1) + "\\");
//write line
blank = "";
k = 0;
while (k < num)
{
blank += " ";
k++;
}
blank += "|----";
last = num;
num += 6;
string[] dirs;
int len = dir.Length;
try
{
dirs = Directory.GetFiles(dir);
foreach (string s in dirs)
{
sw.WriteLine(blank + s.Substring(len + 1));
}
}
catch (Exception e)
{
Console.WriteLine("Execption:{0}", e.Message);
}
try
{
dirs = Directory.GetDirectories(dir);
foreach (string s in dirs)
{
listfile(s);
num -= 6;
last = num - 6;
}
}
catch (Exception e)
{
Console.WriteLine("Execption:{0}", e.Message);
}
}
static void Main(string[] args)
{
sw = File.CreateText("FileList.txt");
try
{
listfile(Directory.GetCurrentDirectory());
}
catch (Exception e)
{
Console.WriteLine("Error {0}. \nPress any key to continue...", e.ToString());
Console.ReadKey();
}
sw.Close();
}
}
}
Tags:
访问文章出处: http://dongnature.spaces.live.com/blog/cns!2CAB6FCCBB15F17B!389.entry(Thinking ...: C#打印树形目录列表.)

上一篇: valu @ 不老歌 - 记录与分享
下一篇: CheckedlistBox绑定数据库,并获得所有选中的值(ValueMember)和显示值(DisplayMember)_Win2050.Com 博客天地 www.inbaidu.com