Tuesday 2 October 2012

How to replace text in file in .Net?

Definition: How to replace text in file?

Method:
1) Add namespace
    using System.IO;

Code:
 protected void btnReplace_Click(object sender, EventArgs e)
    {
        string sfileName = string.Empty;
        sfileName = "F:\\Work\\HTML\\Profile.txt";

        File.WriteAllText(
            sfileName,
            File.ReadAllText(sfileName)
                .Replace("Name", "Abhishek")
                .Replace("Address","Block No.123,LBS Road,Mumbai,Maharastra")
                .Replace("Mobile", "93117481253")
       );

    }


No comments:

Post a Comment