Change a line in a file containing a specific String in C#
<p>ive got a problem: i want to find a line containing a certain string, but i only know how to replace the string in the file or all the lines, i know the command "string.Contains", but it doesnt seem to work properly as i use it: i tried to use "if (data.contains(string))", but then it still changes all the lines to that string. heres my code:</p>

<pre><code>private void button1_Click(object sender, EventArgs e)
{
    string replaceText = "peter";
    string withText = "Gilbert";
    using (System.IO.StreamReader streamReader = new System.IO.StreamReader(@"C:\Users\G\Documents\test.txt"))
    {
        using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(@"C:\Users\G\Documents\test.txt" + ".tmp"))
        {

            while (!streamReader.EndOfStream)
            {
                string data = streamReader.ReadLine();
                data = data.Replace(replaceText, withText);
                streamWriter.WriteLine(data);
            }
        }

    }
    using (System.IO.StreamReader streamReader = new System.IO.StreamReader(@"C:\Users\G\Documents\test.txt" + ".tmp"))
    {
        using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(@"C:\Users\G\Documents\test.txt"))
        {

            while (!streamReader.EndOfStream)
            {
                string data = streamReader.ReadLine();
                data = data.Replace(replaceText, withText);
                streamWriter.WriteLine(data);
            }

        }
    }

}
</code></pre>

<p>}</p>
