Ok so here's the problem, i use a connection with using(connection.... blah blah) and then after my using block is over I want to start the excel aplication like this: System.Diagnostics.Process.Start(excelFile);
This works... sometimes, other times my computer runs too quick and before the file is fully close, or the connection is fully terminated, or something like that, the above statement is exicuted and excel opens and says it cannot access the file.
This happens on and off, if i pause it, it work more often, but i need a way to check to see if i am going to be able to access the file before I access it.
This does not throw an exception, so catching the exception is not an option
i have tried: connection.Close(); connection.Dispose(); GC.Collect(); of which none have worked.
I know that any check will most likley have the possability of returning that the file is available and then before the open statment can be exicued the file being used by someone, thats fine. Just need a way to check.
Ok I tried this:
Microsoft.Office.Interop.Excel.Application _app = new Microsoft.Office.Interop.Excel.Application();
try
{
Workbook wbook = _app.Workbooks.Open(excelFile,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
catch (COMException ex)
{
//The file can't be opened in Excel
}
finally
{
//close the workbook and release the resources (GC.Collect() et al as usual)
_app.Workbooks.Close();
GC.Collect();
}
System.Diagnostics.Process.Start(excelFile);
and i goes through catches the exception and then proceeded to the start command where excel tells me it "cannot access 'fileName'"