0

Let i have program that open file and append something. If i should run two apllication i will get IOException file used by another process. How it's possible to check that file Log.txt is using by another process?

class Program
{
    static void Main(string[] args)
    {
        FileInfo file = new FileInfo(@"D:\Log.txt");
        using (StreamWriter sw = file.AppendText())
        {
            for (int i = 0; i < 1000; i++)
            {
                System.Threading.Thread.Sleep(100);
                sw.WriteLine("Hello");
                sw.WriteLine("And");
                sw.WriteLine("Welcome");
            }
            Console.WriteLine("The work is done");
        }
    }
}
A191919
  • 3,422
  • 7
  • 49
  • 93
  • Do you want to check _if_ the file is used by another process, or do you want to check _what_ process is using it? – Visual Vincent Aug 12 '16 at 08:59
  • @VisualVincent, I want to check if condition. – A191919 Aug 12 '16 at 09:01
  • Then a Try/Catch is your solution. sam has provided a good answer. – Visual Vincent Aug 12 '16 at 09:02
  • Be aware that doing a precheck before doing something with the file isn't helpful. The status of the file can change directly after the check. So when doing something with the file you must handle the case that you can't get the file anyway independed of that check. – Ralf Aug 12 '16 at 09:05
  • 1
    Possible duplicate of [Is there a way to check if a file is in use?](http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use) – MikeT Aug 12 '16 at 09:10

1 Answers1

3

You should try to open and write to the file. If it is in use you get an exception. No other way in .NET.

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

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

    //file is not locked
    return false;
}
Mauro Sampietro
  • 2,739
  • 1
  • 24
  • 50
  • That's right: there's a *time gap* between any possible test and actual writing into the file when some process can start reading/writing from/into the file. – Dmitry Bychenko Aug 12 '16 at 09:10
  • 1
    if you are going to use someone else's answer its good form to include a link to their post http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use – MikeT Aug 12 '16 at 09:18
  • @MikeT answer is mine, the fact that the code i provided has been used in other answers is irrelevant since if you search on stackoverflow or google you will find out the exactly same code is used in 1000s of articles... So who's the owner? Is the link you provided just another user who copied from somewhere? Fruitless and meaningless debate, sorry. – Mauro Sampietro Aug 12 '16 at 09:30