fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. using System.IO;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10. class Program
  11. {
  12. static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
  13.  
  14. static uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
  15.  
  16. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  17. struct WIN32_FIND_DATA
  18. {
  19. public uint dwFileAttributes;
  20. public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
  21. public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
  22. public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
  23. public uint nFileSizeHigh;
  24. public uint nFileSizeLow;
  25. public uint dwReserved0;
  26. public uint dwReserved1;
  27. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  28. public string cFileName;
  29. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
  30. public string cAlternateFileName;
  31. }
  32.  
  33. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  34. static extern IntPtr FindFirstFile(string lpFileName, out WIN32_FIND_DATA lpFindFileData);
  35.  
  36. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  37. static extern bool FindNextFile(IntPtr hFindFile, out WIN32_FIND_DATA lpFindFileData);
  38.  
  39. [DllImport("kernel32.dll")]
  40. public static extern bool FindClose(IntPtr hFindFile);
  41.  
  42. static void Main(string[] args)
  43. {
  44. foreach (var f in enumfiles("C:\\"))
  45. {
  46. Console.WriteLine(f.cFileName);
  47. }
  48. }
  49.  
  50. static IEnumerable<WIN32_FIND_DATA> enumfiles(string dirname)
  51. {
  52. var stack = new Stack<string>();
  53. stack.Push(dirname);
  54.  
  55. while (stack.Count > 0)
  56. {
  57. var present = stack.Pop();
  58.  
  59. WIN32_FIND_DATA win32fd;
  60. IntPtr find = FindFirstFile(Path.Combine(present, "*"), out win32fd);
  61. if (find == INVALID_HANDLE_VALUE)
  62. {
  63. continue;
  64. }
  65.  
  66. do
  67. {
  68. if ((win32fd.cFileName == ".") || (win32fd.cFileName == ".."))
  69. {
  70. continue;
  71. }
  72.  
  73. win32fd.cFileName = Path.Combine(present, win32fd.cFileName);
  74. if ((win32fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
  75. {
  76. yield return win32fd;
  77. }
  78. else
  79. {
  80. stack.Push(win32fd.cFileName);
  81. }
  82. } while (FindNextFile(find, out win32fd));
  83. FindClose(find);
  84. }
  85. }
  86. }
  87. }
  88.  
Runtime error #stdin #stdout #stderr 0.03s 38072KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Unhandled Exception: System.EntryPointNotFoundException: FindFirstFile
  at (wrapper managed-to-native) ConsoleApplication1.Program:FindFirstFile (string,ConsoleApplication1.Program/WIN32_FIND_DATA&)
  at ConsoleApplication1.Program+<enumfiles>c__Iterator0.MoveNext () [0x00000] in <filename unknown>:0 
  at ConsoleApplication1.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.EntryPointNotFoundException: FindFirstFile
  at (wrapper managed-to-native) ConsoleApplication1.Program:FindFirstFile (string,ConsoleApplication1.Program/WIN32_FIND_DATA&)
  at ConsoleApplication1.Program+<enumfiles>c__Iterator0.MoveNext () [0x00000] in <filename unknown>:0 
  at ConsoleApplication1.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0