2

Hi guys I need some help. I'm trying to open a textfile on a servern from multiple clients at the same time so I'm not locking the file when reading from it. Like this:

new StreamReader(File.Open(logFilePath, 
                       FileMode.Open, 
                       FileAccess.Read, 
                       FileShare.ReadWrite))

Now I'm trying to check if this file is used by any of the clients (because I want to write something new to it), but since I'm not locking it when reading from it I don't know how to do this. I can't try to open and catch an exception because it will open.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user2099024
  • 1,452
  • 4
  • 16
  • 26
  • 1
    read this http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use – Roar Apr 16 '13 at 07:29
  • That's showing how to check if a file is in use by checking if it's locked. Not my question – user2099024 Apr 16 '13 at 07:30
  • See http://stackoverflow.com/questions/15362518/checking-if-a-file-is-in-use-without-try-catch – LMeyer Apr 16 '13 at 07:32
  • If your clients aren't able to cope with the file being written to whilst they're reading from it, why on earth are you specifying `FileShare.ReadWrite`? – Damien_The_Unbeliever Apr 16 '13 at 07:48
  • And since each client is acting autonomously, the only viable approach is to attempt to open the file for writing and cope with an exception if its not available for writing - anything else (e.g. check then open) is subject to race conditions. – Damien_The_Unbeliever Apr 16 '13 at 07:50
  • You mean that I should have FileShare.Read? I don't think that matters anyway because the problem is that I don't want to write to it if it's read by any client. I'm working on Tigrans solution right now – user2099024 Apr 16 '13 at 08:00
  • Yes, you should use `FileShare.Read` if you don't want anyone writing to the file. And Tigran's solution has the race condition I mentioned - after `File.Exists()` returns false, and *before* you do anything else, some other piece of code could create the file whose existence you just checked for. "Check, then Act" is always broken if some other piece of code is also running. – Damien_The_Unbeliever Apr 16 '13 at 08:13

2 Answers2

3

Do you can try this?

Or watch this question already asked here ->Is there a way to check if a file is in use?

protected virtual bool IsFileLocked(FileInfo file)
{
    FileStream stream = null;

    try
    {
        stream = file.Open(FileMode.Open, FileAccess.ReadWrite, 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
    {
        if (stream != null)
            stream.Close();
    }

    //file is not locked
    return false;
}
Community
  • 1
  • 1
Mehdi Bugnard
  • 3,889
  • 4
  • 45
  • 86
2

I can't try to open and catch an exception because it will open

Why ? It's a valuable option to work in that way.

By the way you can also create a some empty predefined file, say "access.lock", and others, in order to understand if the actual file is locked check on lock file presence:

if(File.Exist("access.lock")) 
  //locked 
else
  //write something
Tigran
  • 61,654
  • 8
  • 86
  • 123