Solved

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

Posted on 2009-07-02
7
676 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now