刚写出来的代码,测试了下,感觉很方便,给一个根目录就自动获取到下面所有的文件文件夹的树形结构
我是2015年11月21日上午9点36分独立写完并测试的代码,如果已经有人搞出来了那只能是巧合。。
上代码:
public class Folder
{
private string fullPath;
public string FullPath
{
get
{
return this.fullPath;
}
set
{
if (value != this.fullPath)
{
if (!System.IO.Directory.Exists(value))
{
return;
}
this.fullPath = value;
System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(value);
this.FileList = dirInfo.GetFiles()
.Select(fileInfo => fileInfo.Name).ToList();
this.SubFolderList = dirInfo.GetDirectories()
.Select(subDir => new Folder()
{
FullPath = subDir.FullName
}).ToList();
}
}
}
private List<string> fileList;
public List<string> FileList
{
get
{
if (this.fileList == null)
{
this.fileList = new List<string>();
}
return this.fileList;
}
set
{
this.fileList = value;
}
}
private List<Folder> subFolderList;
public List<Folder> SubFolderList
{
get
{
if (this.subFolderList == null)
{
this.subFolderList = new List<Folder>();
}
return this.subFolderList;
}
set
{
this.subFolderList = value;
}
}
}
免去了自己手写递归的麻烦。
.