zoukankan      html  css  js  c++  java
  • 实现文件与目录快速遍历

    快速遍历文件与目录相关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     }
     
  • 相关阅读:
    c学习第6天
    c学习第5天
    c学习第4天
    c学习第1天
    20171009/20171010/20171011
    20171010
    20171008
    20171007
    20171006
    matrix
  • 原文地址:https://www.cnblogs.com/mikechang/p/2696645.html
Copyright © 2011-2022 走看看