Ejemplo n.º 1
0
        /// <summary>
        /// 读取列表
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<FileModel> GetItems(string path)
        {
            if (!Directory.Exists(path))
            {
                return null;
            }
            string[] folders = Directory.GetDirectories(path);
            string[] files = Directory.GetFiles(path);

            List<FileModel> list = new List<FileModel>();
            foreach (string s in folders)
            {
                FileModel item = new FileModel();
                DirectoryInfo di = new DirectoryInfo(s);
                item.Name = di.Name;
                item.FullName = di.FullName;
                item.CreationDate = di.CreationTime;
                item.IsFolder = false;
                //item.Size = GetDirectoryLength(s);
                list.Add(item);
            }
            foreach (string s in files)
            {
                FileModel item = new FileModel();
                FileInfo fi = new FileInfo(s);
                item.Name = fi.Name;
                item.FullName = fi.FullName;
                item.CreationDate = fi.CreationTime;
                item.IsFolder = true;
                item.Size = fi.Length;
                list.Add(item);
            }

            if (path.Replace("\\", "").Replace("/", "").Length != strRootFolder.Replace("\\", "").Replace("/", "").Length + 36)
            {
                FileModel topitem = new FileModel();
                DirectoryInfo topdi = new DirectoryInfo(path).Parent;
                topitem.Name = "[上一级]";
                topitem.FullName = topdi.FullName;
                list.Insert(0, topitem);

                FileModel rootitem = new FileModel();
                DirectoryInfo rootdi = new DirectoryInfo(strRootFolder);
                rootitem.Name = "[根目录]";
                rootitem.FullName = rootdi.FullName;
                list.Insert(0, rootitem);

            }
            return list;
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 读取文件信息
 /// </summary>
 /// <param name="path"></param>
 /// <returns></returns>
 public static FileModel GetItemInfo(string path)
 {
     FileModel item = new FileModel();
     if (Directory.Exists(path))
     {
         DirectoryInfo di = new DirectoryInfo(path);
         item.Name = di.Name;
         item.FullName = di.FullName;
         item.CreationDate = di.CreationTime;
         item.IsFolder = true;
         item.LastAccessDate = di.LastAccessTime;
         item.LastWriteDate = di.LastWriteTime;
         item.FileCount = di.GetFiles().Length;
         item.SubFolderCount = di.GetDirectories().Length;
     }
     else
     {
         FileInfo fi = new FileInfo(path);
         item.Name = fi.Name;
         item.FullName = fi.FullName;
         item.CreationDate = fi.CreationTime;
         item.LastAccessDate = fi.LastAccessTime;
         item.LastWriteDate = fi.LastWriteTime;
         item.IsFolder = false;
         item.Size = fi.Length;
     }
     return item;
 }