Solved

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

Posted on 2009-07-02
7
681 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 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