Check if excel file is available before opening or running

Ok, here's the problem, I am using a connection using (connection .... blah blah), and then after completing my use of the block, I want to start the excel application as follows: System.Diagnostics.Process.Start (excelFile);

It works ... sometimes, sometimes my computer runs too fast and before the file is completely closed, or the connection is completely completed, or something like that, the above statement is exicuted and excel opens and says that it cannot access file.

This happens and turns off if I pause it, it works more often, but I need a way to check if I can access the file before I get it.

This does not throw an exception, so the exception cannot be thrown

I tried:

connection.Close();
connection.Dispose();
GC.Collect();

of which none worked.

I know that any check will be most similar to the possibility of returning to the fact that the file is accessible, and then before the open item can be an exicued file that is used by someone, this is fine. Just need 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 look at the exception, and then go to the start command, where excel tells me that "cannot access" fileName "

+3
source share
5 answers

Try this function to check if the file is open:

public bool IsFileOpen(string path)
{
    FileStream fs = null;
    try
    {
        fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
        return false;
    }
    catch(IOException ex)
    {
        return true;
    }
    finally
    {
        if (fs != null)
            fs.Close();
    }
}
+4
source

I assume that when you write a file, you can easily get its file size. Have you tried to compare these two?

while (fileSize != new FileInfo(excelFile).Length) {
    Thread.Sleep(1000);
}
System.Diagnostics.Process.Start(excelFile);

, , , . , , , .

0

Excel Primary Interop Assemblies:

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

//The file can be opened in Excel if there wasn't a caught exception.
//You can wrap the above in a loop to test more than once as per the other answer

. , , .

0

1:

File.Move(sourceFilename, destinationFilename) 

, . , .

, , - .

2:

    FileStream stream = File.OpenWrite("yourfilename");
    stream.Close();

, .

Excel, .

0

FileSystemWatcher , .

Changed , , . , , , - .

0

Source: https://habr.com/ru/post/1710067/


All Articles