Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void Delete(string pathName, bool subDirectories)
- {
- if (!subDirectories)
- {
- if (Kernel32.RemoveDirectory(pathName) != true)
- {
- throw new Exception("Could not delete directory");
- }
- }
- else
- {
- Kernel32.WIN32_FIND_DATA findDat;
- string fileName = pathName + "\\";
- string _pathName = pathName + "\\*";
- IntPtr fileH = Kernel32.FindFirstFile(_pathName, out findDat);
- if (fileH != new IntPtr(-1))
- {
- _pathName = fileName;
- bool S = true;
- while (S)
- {
- if (Kernel32.FindNextFile(fileH, out findDat))
- {
- if (isDots(findDat))
- continue;
- fileName += findDat.cFileName;
- if (findDat.dwFileAttributes == (uint)Kernel32.FileAttributes.Directory)
- {
- if (!Kernel32.RemoveDirectory(fileName))
- {
- Kernel32.FindClose(fileH);
- //throw new Exception(Marshal.GetLastWin32Error().ToString());
- }
- Kernel32.RemoveDirectory(fileName);
- fileName = _pathName;
- }
- else
- {
- if (findDat.dwFileAttributes == (uint)Kernel32.FileAttributes.Readonly)
- //throw new Exception("Read only file, can not delete");
- continue;
- if (!Kernel32.DeleteFile(fileName))
- {
- Kernel32.FindClose(fileH);
- }
- fileName = _pathName;
- }
- }
- else
- {
- if (Marshal.GetLastWin32Error() == 0x12) // ERROR_NO_MORE_FILES
- S = false;
- else
- Kernel32.FindClose(fileH);
- }
- }
- }
- else
- {
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement