您的位置:首页 > 其它

利用属性获取文件目录

2015-11-21 09:39 176 查看
刚写出来的代码,测试了下,感觉很方便,给一个根目录就自动获取到下面所有的文件文件夹的树形结构


我是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;
}
}
}


免去了自己手写递归的麻烦。

.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: