Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. public static void Delete(string pathName, bool subDirectories)
  2. {
  3. if (!subDirectories)
  4. {
  5. if (Kernel32.RemoveDirectory(pathName) != true)
  6. {
  7. throw new Exception("Could not delete directory");
  8. }
  9. }
  10. else
  11. {
  12. Kernel32.WIN32_FIND_DATA findDat;
  13. string fileName = pathName + "\\";
  14. string _pathName = pathName + "\\*";
  15.  
  16. IntPtr fileH = Kernel32.FindFirstFile(_pathName, out findDat);
  17. if (fileH != new IntPtr(-1))
  18. {
  19. _pathName = fileName;
  20. bool S = true;
  21. while (S)
  22. {
  23. if (Kernel32.FindNextFile(fileH, out findDat))
  24. {
  25. if (isDots(findDat))
  26. continue;
  27. fileName += findDat.cFileName;
  28. if (findDat.dwFileAttributes == (uint)Kernel32.FileAttributes.Directory)
  29. {
  30. if (!Kernel32.RemoveDirectory(fileName))
  31. {
  32. Kernel32.FindClose(fileH);
  33. //throw new Exception(Marshal.GetLastWin32Error().ToString());
  34. }
  35.  
  36. Kernel32.RemoveDirectory(fileName);
  37. fileName = _pathName;
  38. }
  39. else
  40. {
  41. if (findDat.dwFileAttributes == (uint)Kernel32.FileAttributes.Readonly)
  42. //throw new Exception("Read only file, can not delete");
  43. continue;
  44.  
  45. if (!Kernel32.DeleteFile(fileName))
  46. {
  47. Kernel32.FindClose(fileH);
  48. }
  49.  
  50. fileName = _pathName;
  51. }
  52. }
  53. else
  54. {
  55. if (Marshal.GetLastWin32Error() == 0x12) // ERROR_NO_MORE_FILES
  56. S = false;
  57. else
  58. Kernel32.FindClose(fileH);
  59. }
  60. }
  61. }
  62. else
  63. {
  64. return;
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement