I am trying to write in a text file and my condition is if a writing already exists in that file, then it should print 'Another Line Added - The Message'. So I've tried the following but it throws exception 'it is being used by another process' and I can understand I am doing something wrong:

```
public void WriteMessage(string message)
{
   string path = @"C:\Users\AT\Documents\Visual Studio 2013\Projects\Sample_App\Sample_App\app_data\log.txt"; //File path

   FileStream stream;

   if (File.Exists(path))
   {
     stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); //This section used to read and write operations

     using (StreamWriter writer = new StreamWriter(stream)) //Writing words to the text file
     {
       string[] lines = File.ReadAllLines(path); //Here is the exception - It's being used by another process

        if (lines.Length > 0)
        {
           writer.WriteLine("\n" + "Another Line Added - " + message);
           writer.Flush();

           stream.Close();
        }
      }
    }
    else
    {
       /**If text file is created for the first time - Starts**/
       stream = new FileStream(path, FileMode.Create); //This section used to read and write operations

       using (StreamWriter writer = new StreamWriter(stream)) //Writing words to the text file
       {
          writer.WriteLine(message);
          writer.Flush();

          stream.Close();
       }
       /**If text file is created for the first time - Ends**/
    }
}

```

Now what happens the file is created for the first with a text included '**Hello World!!** and when I add another text to it, then I am expecting to get the following output:

**Expected output:**

Hello World!! ---- Only for the first time else

Another Line Added - Hello World!!

Another Line Added - Hello World!!

Another Line Added - Hello World!!

I would expect some ideas or advices to make it work and let me know what I am doing wrong. It's totally a simple one. I would like to do one more thing in the file like the below:

**Output:**

1) Hello World!!

2) Hello World!!

**Note:** In the above output, I am incrementing the numbers whenever I run the console application. That means if the same writing exists, then it should increment by one. I was expecting a simple Linq to do like this and is it possible to do in a simple way: (In this case, do I need to create any model to get over the text file?)

```
var query = (var c in fileName
         select c);

foreach(var item in query)
{
  //Do something here
}

```

I've gone through the followings but unable to get the above work yet:

[MSDN - 01](https://msdn.microsoft.com/en-us/library/6ka1wd3w(v=vs.110).aspx)

[MSDN - 02](https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx?f=255&MSPPError=-2147217396)