locked
File state check - different program behavior depending on server RRS feed

  • Question

  • Hi everybody,

    I have written a program to check if it is possible to open a file in C#.
    If my file is opened in another program (a text editor for instance), it should not be possible and return false, it returns true otherwise.

    It works on my computer but not on my client server.

    protected virtual bool IsFileLocked(FileInfo file)
    {
        try
        {
            using(FileStream stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None))
            {
                stream.Close();
            }
        }
        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;
        }

        //file is not locked
        return false;
    }

    Thanks for your help

    Pete

    Wednesday, August 26, 2020 8:01 PM

Answers

  • Hi, 

    The links below provide some methods to detect whether a file is locked, those methods look better.

    How to check for file lock? [duplicate]

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by CoolDadTx Thursday, August 27, 2020 3:20 PM
    • Marked as answer by ms_bi_pete Thursday, August 27, 2020 8:24 PM
    Thursday, August 27, 2020 9:14 AM
  • Thanks for your accurate answer Michael.

    • Marked as answer by ms_bi_pete Thursday, August 27, 2020 8:24 PM
    Thursday, August 27, 2020 8:24 PM

All replies

  • When you say it's not working on a server, are you performing the test with the same file used locally? Does the catch get triggered on the server and if so what is the exception message?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, August 26, 2020 8:43 PM
  • Hi Karen, 

    Thanks for your help.
    My files are stored locally, let say in C:\Temp\ directory.
    There is no exception, the file.Open operation is a success whereas the file is open in a text editor.

    Regards

    Pete

    Thursday, August 27, 2020 7:35 AM
  • What programs did you test? For example, Notepad does not seem to lock text files.

    In addition, such IsFileLoked function is not reliable, because the status of the file could be changed immediately after this call).



    • Edited by Viorel_MVP Thursday, August 27, 2020 8:15 AM
    • Proposed as answer by CoolDadTx Thursday, August 27, 2020 3:20 PM
    Thursday, August 27, 2020 8:01 AM
  • Hi, 

    The links below provide some methods to detect whether a file is locked, those methods look better.

    How to check for file lock? [duplicate]

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by CoolDadTx Thursday, August 27, 2020 3:20 PM
    • Marked as answer by ms_bi_pete Thursday, August 27, 2020 8:24 PM
    Thursday, August 27, 2020 9:14 AM
  • Your assumption is incorrect. Just because a file is open in another program doesn't mean you cannot open it. It completely boils down to the file share access mode the file is opened in. Some programs open the file for reading (which is almost always valid) and therefore any other process can open the file. The only time these apps will open for writing is on save and that is a finite amount of time. Note that in your case you're using FileShare.None but that is only from this point on, not for any process that already has the file open. Hence if another app now tries to open the file they will get an error but any previous process will still have the file open.

    Also note that these types of checks are almost always wrong anyway. Just because you can open the file now (or not) doesn't mean you will be able to when you actually try. Because every process is running async from everyone else it is very easy to have another process open the file for writing after your check but before you try to open it yourself. The only valid way to handle a "locked file" is to open it for your requested work and handle the exception. It is only at this point that you can make the determination either way. Everything else is just an optimistic check.

    Finally note that your actual check is going to return false positives. IOException is a low level exception. You can get that for a variety of reasons far beyond a file share violation. For example if the file does not exist then you get this exception (at least the derived type version), same for a bad path. AFAIK there isn't a specific exception you can check for this scenario.

    Please use the Insert Code Block tool in the forums to insert code. Your code is harder to read without it. It also keeps the formatting.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, August 27, 2020 3:20 PM
  • Thanks for your accurate answer Michael.

    • Marked as answer by ms_bi_pete Thursday, August 27, 2020 8:24 PM
    Thursday, August 27, 2020 8:24 PM
  • Ty Timon
    Thursday, August 27, 2020 8:25 PM
  • Ty for your help Viorel
    Thursday, August 27, 2020 8:25 PM