利用属性获取文件目录

刚写出来的代码,测试了下,感觉很方便,给一个根目录就自动获取到下面所有的文件文件夹的树形结构生气

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

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


.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值