Solved

Search and replace string in a file using ASP.NET C#

Posted on 2009-07-02
7
680 Views
Last Modified: 2012-05-07
Hi,

I am new to ASP.NET C#.

I want to search for a string and replace that string with another string in a file.
How do I do that?
0
Comment
Question by:meetpd
7 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 24770402
Replace String Examples in C#
http://dotnetperls.com/replace-string-use
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24770405
0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 200 total points
ID: 24770553
try this code
   StreamReader sr;
            StreamWriter sw;
            string strData;
             sr = new StreamReader(@"C:\Test2.txt");
                                    strData = sr.ReadToEnd();
                                    sr.Close();
                                    sw = new StreamWriter(@"C:\Test2.txt");
                                 strData =   strData.Replace("Old", "New"); 
                                
                                    sw.Write(strData);
                                    sw.Flush();
                                    sw.Close();
            sr.Dispose();
            sw.Dispose();

Open in new window

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 3

Assisted Solution

by:coagantus
coagantus earned 100 total points
ID: 24770571
this function get 3 parameters filepath, searchText and replaceText. pas it your required parametrs and enjoy...
static public void ReplaceInFile( string filePath, string searchText, string replaceText )
{
    StreamReader reader = new StreamReader( filePath );
    string content = reader.ReadToEnd();
    reader.Close();
 
    content = Regex.Replace( content, searchText, replaceText );
 
    StreamWriter writer = new StreamWriter( filePath );
    writer.Write( content );
    writer.Close();
}

Open in new window

0
 

Author Comment

by:meetpd
ID: 24771556
How do I replace

<?xml version="1.0"?> with
<?xml version="1.0" encoding="ISO-8859-1" ?>

Because there are double quotes in between, I am not able to figure out how to put them in

  strData =   strData.Replace("Old", "New")   format.

Can you please help?


0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 200 total points
ID: 24771566
You need to use an escape-char

strData =   strData.Replace("<?xml version=\"1.0\"?>", "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>")   format.
0
 

Author Comment

by:meetpd
ID: 24771685
Thanks Everyone for helping me you...

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question