0

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:

  1. DirectoryInfo.Delete(true): ---IOException
  2. Foreach FileInfo and DirectryInfo.Delete ---- same result => IOException
  3. 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
  4. Importing the moveFileEx via DllImport --- but it's not deleting after reboot!
  5. 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(); });

    }
Zilla
  • 1
  • 1
  • there is some code on SO which finds what processes have a specific file open.. also ioexception is vauge there is always a more specific reason. allow for those and fix – BugFinder Apr 25 '18 at 13:40
  • Without seeing one line of your code, nobody will be able to tell you what might be wrong with it. Having said that, compare your MoveFile code with this: https://stackoverflow.com/a/6077952/1220550 – Peter B Apr 25 '18 at 13:47
  • I'm avoiding killing other tasks and thready until now. This would be my last chance. But for example TortoiseSVN i don't know if it is working after killing? I would prefere to not killing other Tasks and Thready until i have other options. – Zilla Apr 25 '18 at 13:52
  • If it's in temp, why do you care so much, actually? It's quite normal that files keep lying around there for some time, until someone starts a disk cleanup. – PMF Apr 25 '18 at 13:57
  • Okay it's not as clear as i though: I must delete the files! It is very important! – Zilla Apr 25 '18 at 13:58
  • @Zilla: Oh, it's something confidential? Then I would not write the information to temp this way. Quite unsafe. Do not cache the files, use memory or encrypt them. – PMF Apr 25 '18 at 14:18
  • This are 20 MB of Files but i could try it. I have already an aes File Encryption working. Perhaps i will write the files encrypted on the temp. --- sometimes you stay too close to the sollution.... – Zilla Apr 25 '18 at 14:28

0 Answers0