How can I delete a file that has been moved in a webbrowser control?

How to delete a file that has been moved in a web browser? The error says: "It is being used by another process."

preview_wb.Navigate(@"C:\mypdf.pdf");

private void close_btn_Click(object sender, EventArgs e)
{
    preview_wb.Stop();
    File.Delete(@"C:\mypdf.pdf");
}
+5
source share
3 answers

Usually people offer this code:

webBrowser.Navigate("about:blank");
while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
    Application.DoEvents();

File.Delete(fileName);

I do not like it. I prefer to handle the DocumentCompleted event.

void DeleteFile()
{    
    needToDeleteFile = true;
    webBrowser.Navigate("about:blank");
}

void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (needToDeleteFile)
    {
        File.Delete(fileName);
        needToDeleteFile = false;
    }
}
+3
source

If you want the “preview” to be saved after deleting the file, you probably have no choice but to copy the file and go to the copy.

If you are happy to clear the "preview" when the file is deleted, first open it:

private void close_btn_Click(object sender, EventArgs e)
{
    preview_wb.Navigate("about:blank");
    File.Delete(@"C:\mypdf.pdf");
}

That should do the trick.

+1

- ( API FindFirst/NextUrlCacheEntry) DeleteUrlCacheEntry? , , Google.

3 , "Cookie:", , ":", ( , ), URL-, http:// https://. , , , .

Let me know if you have any additional questions, so you need to do this to get rid of the problem with the file, if that is not the case, either .dispose or unload your web browser control before doing the cache removal (but you probably won't need it).

0
source

All Articles