writing to a text file
<p>I have a text file with these contents</p>

<pre><code>balamurugan,rajendran,chendurpandian
christopher
updateba
</code></pre>

<p>and i have read these files and searched for a keyword <code>ba</code> and
 i tried to write in another text file <code>log.txt</code> but after executing my code
 i am getting the third line only as </p>

<pre><code>`LineNo : 2 : updateba` 
</code></pre>

<p>I need to get both these lines</p>

<pre><code>LineNo : 0 : balamurugan,rajendran,chendurpandian
LineNo : 2 : updateba
</code></pre>

<p>I am using this code to write to a text file </p>

<pre><code>if (File.Exists(FilePath))
        {
            // Read the file and display it line by line.
            System.IO.StreamReader file = new System.IO.StreamReader(FilePath);
            while ((line = file.ReadLine()) != null)
            {
                if (line.Contains(regMatch))
                {
                    DirectoryInfo Folder = new DirectoryInfo(textboxPath.Text);
                    if (Folder.Exists)
                    {
                        var dir = @"D:\New folder\log";
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }
                        File.WriteAllText(Path.Combine(dir, "log.txt"), "LineNo : " + counter.ToString() + " : " + line + "&lt;br /&gt;");

                    }
                    else
                    {
                        Response.Write("&lt;script language='javascript'&gt;window.alert('Folder not found');&lt;/script&gt;");
                    }
                    Response.Write("&lt;script language='javascript'&gt;window.alert('Pattern found');&lt;/script&gt;");
                    Response.Write("LineNo : " + counter.ToString()+ " : " + line + "&lt;br /&gt;");
                }

                else
                {
                    Response.Write("&lt;script language='javascript'&gt;window.alert('Pattern not found');&lt;/script&gt;");
                }
                counter++;


            }
               file.Close();
        }
        else
        {
            Response.Write("&lt;script language='javascript'&gt;window.alert('File not found');&lt;/script&gt;");
        }
</code></pre>

<p>i have used this sample<a href="http://stackoverflow.com/questions/2437716/search-text-file-using-c-and-display-the-line-number-and-the-complete-line-that">link text</a></p>

<p>Any suggestion???</p>
