1

i have written some pdf files to a temp directory and these get displayed as a thumbnail that the user can view. when i close my form i clean up all of the files in the temp directory.

If however the user has one of the thumbnails open and then closes my application - it deletes the files and then throws an exception because the pdf is open in another process and cant be cleaned up.

I guess this is a shocking programming decision by me, but i am still a novice! How should i account for this in my code?

Thanks

rik
  • 1,279
  • 4
  • 20
  • 29
  • simply you can't. If file is opened you can't delete it. You can tell system to delete it on next reboot or you can show the user a message telling him to close that file and keep you app waiting until this happens... – Marco Jun 03 '11 at 08:47
  • Can you let us know how you are cleaning up the files? perhaps, you would have something to dispose – Coder323 Jun 03 '11 at 08:49
  • 1
    There are few existing questions on this topic. http://stackoverflow.com/questions/5724097/delete-file-when-it-used-in-another-process http://stackoverflow.com/questions/4323697/is-it-possible-to-delete-a-file-that-is-opened-by-a-process-under-windows http://stackoverflow.com/questions/5724097/delete-file-when-it-used-in-another-process Looking at the posts, it looks like it is not possible. – Sandeep G B Jun 03 '11 at 08:49
  • See this too: http://stackoverflow.com/questions/1040/how-do-i-delete-a-file-which-is-locked-by-another-process-in-c – blizpasta Jun 03 '11 at 08:50
  • is it not possible to show a message to the user saying, this file needs to be closed save if you want it or close if not? – rik Jun 03 '11 at 08:56

3 Answers3

2

You can detect if the file is in use by using code similar to below, then use that to warn the user that a file can't be deleted. Unfortunately you can't delete a file that is in use.

    public static bool IsFileInUse(string pathToFile)
    {
        if (!System.IO.File.Exists(pathToFile))
        {
            // File doesn't exist, so we know it's not in use.
            return false;
        }

        bool inUse = false;
        System.IO.FileStream fs;
        try
        {
            fs = System.IO.File.Open(pathToFile, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read, System.IO.FileShare.None);
            fs.Close();
        }
        catch (System.IO.IOException ex)
        {
            string exMess = ex.Message;
            inUse = true;
        }
        return inUse;
    }
IndigoDelta
  • 1,481
  • 9
  • 11
0

You should catch that exception (in catch block you can inform user to close that file or it will not be deleted), and if the temp directory is yours you can try to delete it when application starts (or when it ends again), if its windows temp directory, then it does not matter that much

Kikaimaru
  • 1,841
  • 1
  • 16
  • 28
  • it kind of matters because the next time the user searches for data this pdf will pop up and it wont relate to the search they did – rik Jun 03 '11 at 09:00
0

Tools like File Unlocker can release a file. However I think this could make programs depending on the file crash...

Maybe you can look up how they unlock files or manage to execute the unlocker via Process.Start to unlock your file and delete it.

However if it's you blocking the file you should try and fix this in your programm. Maybe you should dispose all loaded files (filestreams etc) before trying to clean it up.

Zebi
  • 8,682
  • 1
  • 36
  • 42