0

I'm using the below method to check if a file is already in use... seems to work just fine but my problem is that if I have excel open, sometimes the filestream actually opens and leaves open the excel file that its currently checking.

Anyone know why the file is actually opening and not closing when it leaves the using statement?

EDIT: More to the above, it opens the excel file and then it display a message box telling me that the file is available to Read/Write

I put the below in the comments but I think it pertains to my usage of the IsFileLocked code and may actually be the real cause??

I'm using the IsFileLocked code along with a filewatcher. When the filewatcher goes off I check if the excel file is still in use and if its not in use then I query data back from that excel file.

Process.. 1.) Excel file has changed or is being changed by another user 2.) make sure its still not in use with the IsFileLocked method 3.) if file is no longer in use then query data back from the excel file but sometimes the excel file actually opens on the user's computer who is watching excel files for changes.. if this makes sense

    protected virtual bool IsFileLocked(string path)
    {
        try
        {
            using (FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None))
            {

            }
        }
        catch (IOException)
        {
            //the file is unavailable because it is:
            //still being written to
            //or being processed by another thread
            //or does not exist (has already been processed)
            return true;
        }
        finally
        {
        }
        //file is not locked
        return false;
    }
ExcelNoobie25
  • 39
  • 1
  • 7
  • *"sometimes the filestream actually opens and leaves open the excel file that its currently checking."* what exactly do you mean by that? It's really not clear. – Matt Burland Aug 31 '16 at 20:42
  • I'm assuming the code in the question was adapted from here: http://stackoverflow.com/a/937558/1250301, but you really need to pay attention to this one: http://stackoverflow.com/a/876513/1250301 – Matt Burland Aug 31 '16 at 20:46
  • This works as expected for me. The only obvious reason why it might not is because Excel didn't take a lock on the file in the first place. For example, if the file is read-only, then it won't be locked by Excel – Matt Burland Aug 31 '16 at 20:54
  • 1
    From the above link by Matt Burland, it sounds like just try to open the file and if it fails then just let the user know. – KSib Aug 31 '16 at 20:58
  • It's also possible (although I doubt this is the case) when opening a file directly downloaded from your browser. It will open in "protected view" which doesn't lock. – Matt Burland Aug 31 '16 at 21:00
  • I'm using the above code along with a filewatcher. When the filewatcher goes off I check if the excel file is still in use and if its not in use then I query data back from that excel file. – ExcelNoobie25 Aug 31 '16 at 21:15
  • Process.. 1.) Excel file is changed or changing by another user 2.) make sure its still not in use with the is fileopen method 3.) if file is no longer in use then query databack from the excel file but sometimes the excel file actually opens on the user's computer who is watching excel files for changes.. if this makes sense – ExcelNoobie25 Aug 31 '16 at 21:16
  • @ExcelNoobie25: *"the excel file actually opens on the user's computer who is watching excel files for changes"*, so it would seem the problem is probably on that end. There isn't enough information in your question to hazard a guess as we don't know what you are doing on the *other* computer. – Matt Burland Sep 01 '16 at 13:24

0 Answers0