My problem is that I have to delete files and folders on the machine, which I created myself. Nothing special, but I'm using different tools (TortoiseSVN, special Custom Tools, etc...) which I cannot guarantee that they are all closed after closing my own application. (for example: TortoiseSVN runs always)
I'm working in the Windows Temp Folder in AppData/Local/temp and perhaps Virus-Scanner are blocking the directory too.
What I have already tried:
- DirectoryInfo.Delete(true): ---
IOException
- Foreach FileInfo and DirectryInfo.Delete ---- same result =>
IOException
- Save the Files and Folders in a List and polling endless to delete --- best result, but if the user is shutdown first, the files and folders kept in Temp
- Importing the moveFileEx via DllImport --- but it's not deleting after reboot!
- Try moveFileEx on each File and Fold (Files first) --- the same result, not deleted
Before I'm starting with creating an Windows Service to delete all files, I hope we can find another solution.
PS: Testing on Windows 10 64 Bit and writing in C# WPF Visual Studio Prof. 2017
My moveFileEx Code:
internal enum MoveFileFlags
{
MOVEFILE_REPLACE_EXISTING = 1,
MOVEFILE_COPY_ALLOWED = 2,
MOVEFILE_DELAY_UNTIL_REBOOT = 4,
MOVEFILE_WRITE_THROUGH = 8,
MOVEFILE_CREATE_HARDLINK_RESERVER = 16,
MOVEFILE_FAIL_IF_NOT_TRACKABLE = 32
}
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, MoveFileFlags dwFlags);
public static void MoveFileUntilReboot(this FileInfo source , FileInfo Destination)
{
MoveFileEx(source.FullName, Destination.FullName, MoveFileFlags.MOVEFILE_DELAY_UNTIL_REBOOT);
}
public static void DeleteFileUntilReboot(this FileInfo toDelete)
{
MoveFileEx(toDelete.FullName, null, MoveFileFlags.MOVEFILE_DELAY_UNTIL_REBOOT);
}
public static void MoveDirUntilReboot(this DirectoryInfo source, DirectoryInfo Destination)
{
MoveFileEx(source.FullName, Destination.FullName, MoveFileFlags.MOVEFILE_DELAY_UNTIL_REBOOT);
}
public static void DeleteDirUntilReboot(this DirectoryInfo source)
{
if (source == null) return;
if (source.Exists == false) return;
Parallel.ForEach(source.GetDirectories(), (dir) => { dir.DeleteDirUntilReboot(); });
Parallel.ForEach(source.GetFiles(), (File) => { File.DeleteFileUntilReboot(); });
}