日期:2014-05-19  浏览次数:20975 次

显示并按原有顺序打印C:\下所有的目录
显示并按原有顺序打印C:\下所有的目录,请高手赐教!
高分相求!!

------解决方案--------------------
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if( ! this.IsPostBack)
{
String[] Drives;
Drives=System.IO.Directory.GetLogicalDrives();
System.IO.DirectoryInfo dir=new System.IO.DirectoryInfo(Drives[0]);
TreeNode tn=new TreeNode();
tn.Text=Drives[0];
this.TreeView1.Nodes.Add(tn);
this.bind(dir,tn.Nodes);

}

}


}
void bind(System.IO.DirectoryInfo dirs,TreeNodeCollection nodes)
{
TreeNode node=new TreeNode();
node.Text=dirs.Name;
nodes.Add(node);
System.IO.DirectoryInfo[] dir=dirs.GetDirectories();
foreach (System.IO.DirectoryInfo d in dir)
{
this.bind(d, node.Nodes);

}

}
获取所有目录.. 打印自己写
------解决方案--------------------

------解决方案--------------------
学习

------解决方案--------------------
http://www.itepub.net/html/article/bianchengkaifa/cnetok/qitawenzhang/2006/0428/2243.html
------解决方案--------------------
dir/s
------解决方案--------------------
private void ListTreeShow(DirectoryInfo theDir, int nLevel)//递归目录,开始层级
{
DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录
foreach (DirectoryInfo dirinfo in subDirectories)
{
FileInfo[] fileInfo = dirinfo.GetFiles(); //目录下的文件
foreach (FileInfo fInfo in fileInfo)
{
Response.Write(nLevel+ fInfo.Name.ToString() + "[ " + Math.Round((double)fInfo.Length / 1024, 1) + "K] " + "\r\n ");
}
ListTreeShow(dirinfo, nLevel + 1);
}
}

--调用
ListTreeShow(@ "C:\ ",0);
------解决方案--------------------
楼上的调用有问题
改成
System.IO.DirectoryInfo dir1=new System.IO.DirectoryInfo(@ "C:\ ");
ListTreeShow(dir1,0);
------解决方案--------------------
private void getDirectories(string path)
{
string[] fileNames = Directory.GetFiles(path);
string[] directories = Directory.GetDirectories(path);
foreach (string file in fileNames)
{
Console.WriteLine( "Filename:{0} ", file);
}
foreach (string dir in directories)
{
Console.WriteLine( "Directoriesname:{0} ", dir);
getDirectories(dir);
}
}