zoukankan      html  css  js  c++  java
  • 获取指定文件夹内的文件

    首先需要定义文件信息记录类:

     1     public class FileData
     2     {
     3         private string myFullName;
     4         private string myName;
     5         private string myExtension;
     6         private long myLength;
     7 
     8         public  FileData(){}
     9 
    10         public  FileData( string strFullName, string strName, string strExtension, long intLength )
    11         {
    12             this.myFullName = strFullName;
    13             this.myName = strName;
    14             this.myExtension = strExtension;
    15             this.myLength = intLength;
    16         }
    17 
    18         public string FullName
    19         {
    20             get
    21             {
    22                 return this.myFullName;
    23             }
    24         }
    25 
    26         public string Name
    27         {
    28             get
    29             {
    30                 return this.myName;
    31             }
    32         }
    33 
    34         public string Extension
    35         {
    36             get
    37             {
    38                 return this.myExtension;
    39             }
    40         }
    41 
    42         public long Length
    43         {
    44             get
    45             {
    46                 return this.myLength;
    47             }
    48         }
    49 
    50         public override string ToString()
    51         {
    52             return this.myFullName;
    53         }
    54     }

    定义查找文件类(可递归):

      1     public class FileExplorer
      2     {
      3         public ArrayList FileList = new ArrayList();
      4         public ArrayList Extensions = null;
      5         private string Path = "";
      6         private bool Recursive = true;
      7 
      8         public FileExplorer( string path  )
      9         {
     10             this.Path = path;
     11             this.Recursive = false;
     12             this.FileList = this.getFiles();
     13         }
     14 
     15         public FileExplorer( string path, ArrayList extensions  )
     16         {
     17             this.Path = path;
     18             this.Extensions = extensions;
     19             this.Recursive = false;
     20             this.FileList = this.getFiles();
     21         }
     22 
     23         public FileExplorer( string path, bool recursive  )
     24         {
     25             this.Path = path;
     26             this.Recursive = recursive;
     27             this.FileList = this.getFiles();
     28         }
     29 
     30         public FileExplorer( string path, ArrayList extensions, bool recursive  )
     31         {
     32             this.Path = path;
     33             this.Extensions = extensions;
     34             this.Recursive = recursive;
     35             this.FileList = this.getFiles();
     36         }
     37 
     38         public ArrayList getFiles()
     39         {
     40             DirectoryInfo   dir = new DirectoryInfo( this.Path );
     41             FileInfo[]      files;
     42             DirectoryInfo[] dirs;
     43             ArrayList List = new ArrayList();
     44                    if (! dir.Exists)
     45             {
     46                 throw new Exception("文件夹不存在: " + this.Path );
     47             }
     48 
     49             files = dir.GetFiles();
     50 
     51             foreach(FileInfo file in files)
     52             {
     53                 if( this.Extensions == null )
     54                 {
     55                     List.Add( new FileData( file.FullName, file.Name, file.Extension, file.Length ) );
     56                 }
     57                 else 
     58                 {
     59                     if( this.checkFile( file.Extension ) )
     60                     {
     61                         List.Add( new FileData( file.FullName, file.Name, file.Extension, file.Length ) );
     62                     }
     63                 }
     64             }
     65 
     66             files = null;
     67         
     68             if (! this.Recursive)
     69             {
     70                 return List;
     71             }
     72 
     73             dirs = dir.GetDirectories();
     74 
     75             foreach(DirectoryInfo subdir in dirs)
     76             {
     77                 this.Path = subdir.FullName;
     78                 ArrayList temp = new ArrayList();
     79                 temp = getFiles();
     80                 for(int i=0; i < temp.Count; i++)
     81                 {
     82                     FileData TempData = (FileData)temp[i];
     83                     List.Add( new FileData( TempData.FullName, TempData.Name, TempData.Extension, TempData.Length ) );
     84                 }
     85             }
     86         
     87             dirs = null;
     88         
     89             dir = null;
     90 
     91             return List;
     92         }
     93 
     94         private bool checkFile( string strExtension )
     95         {
     96             bool throwaway = true;
     97             for(int j=0; j<this.Extensions.Count; j++)
     98             {
     99                 if( this.Extensions[j].ToString() == strExtension )
    100                 {
    101                     throwaway = false;
    102                 }
    103             }
    104 
    105             if( throwaway )
    106             {
    107                 return false;
    108             }
    109             else
    110             {
    111                 return true;
    112             }
    113         }
    114 
    115     }    
  • 相关阅读:
    json-lib 中关于null与"null"
    Android SDK及Build版本配置说明
    WebStorm下Webpack的Source map问题
    简述Javascript的原型链
    Hbuilder中添加Babel自动编译
    理解Java的lamda表达式实现
    CountDownLatch多个主线程等待示例
    关于CyclicBarrier的执行顺序
    【转载】让Go2Shell支持ITerm2 和x-term
    【原创】mac下为eclipse安装反编译插件
  • 原文地址:https://www.cnblogs.com/ziranquliu/p/4719649.html
Copyright © 2011-2022 走看看