I'm writing a function that creates an html file, loads it into a invisible browser, prints, then deletes that html file. However I'm getting an error in the print and delete portion.
Here are the two errors
(Output from the debugger)
A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
(When error catching is disabled)
The process cannot access the file 'C:\*Path excluded in post* because it is being used by another process.
Here is the code for the Delete function
private void Delete_Invoice(int Invoice_Num)
{
if (File.Exists("Invoice" + Invoice_Num + ".html"))
{
//Delete the invoice file
try
{
File.Delete("Invoice" + Invoice_Num + ".html");
}
catch
{ //System.Data.SqlClient.SqlException ex)
Error newError = new Error();
newError.Show();
newError.txtError.Text =
"There was an error Deleting the file. ";
}
}
}
Code for the Print Function
private void btnPrint_Click(object sender, EventArgs e)
{
//print out all the invoices selected
if (File.Exists("Invoice" + Invoice_Num + ".html"))
{
Delete_Invoice(Invoice_Num);
}
Save_Invoice(Invoice_Num, Serial_Num);
Thread.Sleep(500);
webIn.Navigate(Path.Combine(Application.StartupPath, "Invoice" + Invoice_Num + ".html"));
webIn.Print();
webIn.GoHome();
Thread.Sleep(1000);
Delete_Invoice(Invoice_Num);
}