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);

        }

I do the same thing and have the same problem. What I do is use a prefix on filenames in the %TEMP% directory and when ever I go to create a new file i try to delete the existing ones. If it is in use then it skips it.. when the browser closes and the PC reboots then it will clean up the file eventually. You can name your file something like [ASSEMBLYNAME]_ + Guid.NewGuid().ToString() + ".html" .. then search for the naming prefix.

i think the problem is in the code where you create the html file. probably you work with some streamwriter and you forgot to close the stream, so it does not allow you to delete the file without closing the stream.
post the code that you use to create the html file.

commented: good idea +3

i think the problem is in the code where you create the html file. probably you work with some streamwriter and you forgot to close the stream, so it does not allow you to delete the file without closing the stream.
post the code that you use to create the html file.

Valid point

what is this webin object that is used to load the html? It seems to be already created before the function that calls the print. you could try creating it loading, printing, then disposing that object. Its likely that its still treating the file like its still in use even though you call its own method that seems to navigate away.

Just a though, if you haven't tried that yet.

Sorry it took me so long to reply but here it goes....
The streamwriter is closed, here is the function for that

public void Save_Invoice(int Invoice_Num, int Serial_Num)
        {  
            //fills out the variables for the html file from database
            cur_invoice.Fill_Invoice(Serial_Num, Invoice_Num);
            //convert some of the variables to currency. 
            cur_invoice.Convert_C();
            //create the file (invoice)
            try
            {
                FileInfo Invoice_File = new FileInfo("Invoice" + Invoice_Num + ".html");
            }
            catch 
            {
                Error newError = new Error();
                newError.Show();
                newError.txtError.Text =
                   "There was an error saving the file. ";
            }
            ////////////////////////////////////////////////
            //produce the html to insert into the document./
            ////////////////////////////////////////////////                       
            //Heading and Company Block
            
            System.IO.StreamWriter Out_File = new System.IO.StreamWriter("Invoice" + Invoice_Num + ".html");
            Out_File.WriteLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
            Out_File.WriteLine("<html xmlns=\"http://www.w3.org/1999/xhtml\" ><head><title>Invoice ");
            Out_File.WriteLine(Invoice_Num);
            Out_File.WriteLine("</title>");
            Out_File.WriteLine("</head><body><table width=\"100%\" id=\"CompanyBlock\"><tr> <td align=\"center\"><h1><label id=\"Company\">Holley Office Products, Inc</label></h1></td></tr><tr><td align=\"center\"><label id=\"Street\">P.O. Box 938</label></td></tr><tr><td align=\"center\"><label id=\"City\">Benton</label>,<label id=\"State\">KY</label><label id=\"Zip\">42025</label></td></tr><tr><td align=\"center\"><label id=\"Phone\">(270) 527-5536</label></td></tr></table><table width=\"100%\"><tr><td align=\"center\"><h1><label id=\"CompanyTitle\">COIN OP INVOICE</label></h1></td></tr></table>");
...
...
...
            //Closing html
            Out_File.WriteLine("</td></tr></table></body></html>");

            //close the file
            Out_File.Close();

For the webin object... it is the web browser control i have on the form.

Thank you for your help... I am going to move on to Diamond's suggestion and be back.

ok so my search for how to use the webcontrol landed me here. Microsoft's how to website for te web control. I then was looking deeper and found that I can build a string and load that into the browser than strictly an html file. I am going to go that route and thnks again for all the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.