0

This always returns false regardless of whether file is already open or closed.

It worked fine until yesterday; today it doesn't work as expected.

public bool isOpen(FileInfo file)
{
    FileStream stream = null;

    try
    {
        stream = file.Open(FileMode.Open);
    }
    catch (Exception ex)
    {
        Logger.Error(ex.Message);
        return true;
    }
    finally
    {
        if (stream != null)
            stream.Close();
    }
    return false;
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • Try to search for StackOverflow for "check file open [c#]" - there are already a couple of related questions with answers. Note that checking if a file is open before doing or not doing something on it is usually moot - if that is what you are really after - because that information is already obsolete once you have it. For details, also see answers/comments to said other/related questions. – Christian.K Sep 22 '15 at 13:59
  • Did you change your environment from debug to release? The compiler may be noticing that you don't use stream anyway and basically reducing the function to `return false`. – Ron Beyer Sep 22 '15 at 14:02
  • As an aside, use a [`using`](https://msdn.microsoft.com/en-us/library/yh598w02.aspx) statement to guarantee disposal of the stream. – Wai Ha Lee Sep 22 '15 at 14:06

0 Answers0