Esempio n. 1
0
        /// <summary>
        /// 获取指定目录下所有文件信息
        /// </summary>
        /// <param name="path"></param>
        /// <param name="extensions">文件类型集合,多个以英文逗号分隔。如:.jpg;.gif;.rar;</param>
        /// <returns></returns>
        public static IList <FolderFileInfo> GetFils(string path, string extensions)
        {
            IList <FolderFileInfo> listFolderFileInfo = new List <FolderFileInfo>();

            try
            {
                DirectoryInfo thisOne  = new DirectoryInfo(path);
                int           id       = 1;
                FileInfo[]    fileInfo = thisOne.GetFiles();
                string[]      exten    = extensions.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (FileInfo fInfo in fileInfo)
                {
                    //获取指定文件类型的文件
                    if (exten.Length > 0)
                    {
                        foreach (string item in exten)
                        {
                            if (fInfo.Extension.ToLower() == item.ToLower())
                            {
                                FolderFileInfo folderFileInfo = new FolderFileInfo();
                                folderFileInfo.Id         = id;
                                folderFileInfo.CreateTime = fInfo.CreationTime;
                                folderFileInfo.Extension  = fInfo.Extension;
                                folderFileInfo.FileSize   = (int)fInfo.Length;
                                folderFileInfo.FileUrl    = path + fInfo.Name + fInfo.Extension;
                                folderFileInfo.Folder     = fInfo.DirectoryName;
                                folderFileInfo.Name       = fInfo.Name;
                                folderFileInfo.FullName   = fInfo.FullName;
                                listFolderFileInfo.Add(folderFileInfo);
                                id++;
                                break;
                            }
                        }
                    }
                    else//获取所有文件
                    {
                        FolderFileInfo folderFileInfo = new FolderFileInfo();
                        folderFileInfo.Id         = id;
                        folderFileInfo.CreateTime = fInfo.CreationTime;
                        folderFileInfo.Extension  = fInfo.Extension;
                        folderFileInfo.FileSize   = (int)fInfo.Length;
                        folderFileInfo.FileUrl    = path + fInfo.Name + fInfo.Extension;
                        folderFileInfo.Folder     = fInfo.DirectoryName;
                        folderFileInfo.Name       = fInfo.Name;
                        folderFileInfo.FullName   = fInfo.FullName;
                        listFolderFileInfo.Add(folderFileInfo);
                        id++;
                    }
                }
            }
            catch { }
            return(listFolderFileInfo);
        }
 /// <summary>
 /// 获取指定目录下所有文件信息
 /// </summary>
 /// <param name="path"></param>
 /// <param name="extensions">文件类型集合,多个以英文逗号分隔。如:.jpg;.gif;.rar;</param>
 /// <returns></returns>
 public static IList<FolderFileInfo> GetFils(string path, string extensions)
 {
     IList<FolderFileInfo> listFolderFileInfo = new List<FolderFileInfo>();
     try
     {
         DirectoryInfo thisOne = new DirectoryInfo(path);
         int id = 1;
         FileInfo[] fileInfo = thisOne.GetFiles();
         string[] exten = extensions.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
         foreach (FileInfo fInfo in fileInfo)
         {
             //获取指定文件类型的文件
             if (exten.Length > 0)
             {
                 foreach (string item in exten)
                 {
                     if (fInfo.Extension.ToLower() == item.ToLower())
                     {
                         FolderFileInfo folderFileInfo = new FolderFileInfo();
                         folderFileInfo.Id = id;
                         folderFileInfo.CreateTime = fInfo.CreationTime;
                         folderFileInfo.Extension = fInfo.Extension;
                         folderFileInfo.FileSize = (int)fInfo.Length;
                         folderFileInfo.FileUrl = path + fInfo.Name + fInfo.Extension;
                         folderFileInfo.Folder = fInfo.DirectoryName;
                         folderFileInfo.Name = fInfo.Name;
                         folderFileInfo.FullName = fInfo.FullName;
                         listFolderFileInfo.Add(folderFileInfo);
                         id++;
                         break;
                     }
                 }
             }
             else//获取所有文件
             {
                 FolderFileInfo folderFileInfo = new FolderFileInfo();
                 folderFileInfo.Id = id;
                 folderFileInfo.CreateTime = fInfo.CreationTime;
                 folderFileInfo.Extension = fInfo.Extension;
                 folderFileInfo.FileSize = (int)fInfo.Length;
                 folderFileInfo.FileUrl = path + fInfo.Name + fInfo.Extension;
                 folderFileInfo.Folder = fInfo.DirectoryName;
                 folderFileInfo.Name = fInfo.Name;
                 folderFileInfo.FullName = fInfo.FullName;
                 listFolderFileInfo.Add(folderFileInfo);
                 id++;
             }
         }
     }
     catch { }
     return listFolderFileInfo;
 }