实现文件与目录快速遍历

快速遍历文件与目录相关API
 1   class Program
 2     {
 3          static  void Main( string[] args)
 4         {
 5             DateTime mytime = DateTime.Now;
 6             Program myProgram =  new Program();
 7             myProgram.FindFileInDir( @" \\172.20.1.10\BOM表\ ");
 8             Console.WriteLine(DateTime.Now.Subtract(mytime).Seconds);
 9         }
10 
11 
12         [Serializable,System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential,CharSet = System.Runtime.InteropServices.CharSet.Auto),System.Runtime.InteropServices.BestFitMapping( false)]
13          private  struct WIN32_FIND_DATA
14         {
15              public  int dwFileAttributes;
16              public  int ftCreationTime_dwLowDateTime;
17              public  int ftCreationTime_dwHighDateTime;
18              public  int ftLastAccessTime_dwLowDateTime;
19              public  int ftLastAccessTime_dwHighDateTime;
20              public  int ftLastWriteTime_dwLowDateTime;
21              public  int ftLastWriteTime_dwHighDateTime;
22              public  int nFileSizeHigh;
23              public  int nFileSizeLow;
24              public  int dwReserved0;
25              public  int dwReserved1;
26             [System.Runtime.InteropServices.MarshalAs
27               (System.Runtime.InteropServices.UnmanagedType.ByValTStr,
28               SizeConst =  260)]
29              public  string cFileName;
30             [System.Runtime.InteropServices.MarshalAs
31               (System.Runtime.InteropServices.UnmanagedType.ByValTStr,
32               SizeConst =  14)]
33              public  string cAlternateFileName;
34         }
35         [System.Runtime.InteropServices.DllImport( " kernel32.dll ", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError =  true)]
36          private  static  extern IntPtr FindFirstFile( string pFileName,  ref WIN32_FIND_DATA pFindFileData);
37         [System.Runtime.InteropServices.DllImport( " kernel32.dll ", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError =  true)]
38          private  static  extern  bool FindNextFile(IntPtr hndFindFile,  ref WIN32_FIND_DATA lpFindFileData);
39         [System.Runtime.InteropServices.DllImport( " kernel32.dll ", SetLastError =  true)]
40          private  static  extern  bool FindClose(IntPtr hndFindFile);
41 
42          // 具体方法函数
43 
44         Stack< string> m_scopes =  new Stack< string>();
45          private  static  readonly IntPtr INVALID_HANDLE_VALUE =  new IntPtr(- 1);
46         WIN32_FIND_DATA FindFileData;
47          private System.IntPtr hFind = INVALID_HANDLE_VALUE;
48          void FindFileInDir( string rootDir)
49         {
50              int count =  0;
51              string path = rootDir;
52         start:
53              new FileIOPermission(FileIOPermissionAccess.PathDiscovery, Path.Combine(path,  " . ")).Demand();
54              if (path[path.Length -  1] !=  ' \\ ')
55             {
56                 path = path +  " \\ ";
57             }
58              // Console.WriteLine("文件夹为:" + path + "<br>");
59              hFind = FindFirstFile(Path.Combine(path,  " * "),  ref FindFileData);
60              if (hFind != INVALID_HANDLE_VALUE)
61             {
62                  do
63                 {
64                      if (FindFileData.cFileName.Equals( @" . ") || FindFileData.cFileName.Equals( @" .. "))
65                          continue;
66                      if ((FindFileData.dwFileAttributes &  0x10) !=  0)
67                     {
68                         m_scopes.Push(Path.Combine(path, FindFileData.cFileName));
69                     }
70                      else
71                     {
72                         Regex mygex =  new Regex( " ^.*5233.*\\.xls$ ");
73                          if (mygex.IsMatch(FindFileData.cFileName))
74                         {
75                             Console.WriteLine(path +  " \\ " + FindFileData.cFileName);
76                             count = count +  1;
77                         }
78                     }
79                 }
80                  while (FindNextFile(hFind,  ref FindFileData));
81             }
82             FindClose(hFind);
83              if (m_scopes.Count >  0)
84             {
85                 path = m_scopes.Pop();
86                  goto start;
87             }
88 
89             Console.WriteLine(count.ToString());
90         }
91     }
 

转载于:https://www.cnblogs.com/mikechang/archive/2012/09/21/2696645.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值