Cannot delete file becouse it is used by another process.

Takeshi 41 Reputation points
2020-06-04T09:34:43.807+00:00

Hi all,
I dont understand my problem. In DocumentPage I upload pdf file ->I show it in dataGrid -> after select this dtg row ti create pdf file in some folder and at the same time it show it in webBrowser. After exit applicatin I am trying to delete this file but it shows me en arror mentioned in question title.

my exit function:

App.xaml

<Application ...
    Exit="App_Exit"
>
...
</Application>

App.xaml.cs

public partial class App : Application
    {
        void App_Exit(object sender, ExitEventArgs e)
        {
            DocumentPage z = new DocumentPage();
            z.clear();
            if (Directory.Exists("temp"))
            {
                Directory.Delete("temp", true);
            }
     }
}

... here it shows me that eror message

  1. In same page (DocumentPage) I have button with function ( clear() ) : pdfDokument.Navigate("about:blank");

after click on that button ... application exit and sucessfull delete folder temp with all files inside

SO I dont understand why it is ok with click on that button and when I use this function in app_exit ... it doesnt work

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
37,628 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Vahid Ghafarpour 20,480 Reputation points
    2023-09-09T13:51:38.9233333+00:00

    When you click the button in your DocumentPage to clear the pdfDokument, the application is still running, and the DocumentPage is still active. Therefore, the folder and files can be deleted successfully because the application is not in the process of shutting down.

    0 comments No comments