How can I delete the file that has been navigated in the webbrowser? 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");
}
Usually people suggest this code:
webBrowser.Navigate("about:blank");
while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
File.Delete(fileName);
I don't like it. I prefer to handle DocumentCompleted event.
void DeleteFile()
{
needToDeleteFile = true;
webBrowser.Navigate("about:blank");
}
void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (needToDeleteFile)
{
File.Delete(fileName);
needToDeleteFile = false;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With