Solved

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

Posted on 2009-07-02
7
677 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS ReportViewer report timeout 7 99
error on the asp.net page 1 32
C# Offline Apllication 5 48
Syntax Error 2 41
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

943 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

16 Experts available now in Live!

Get 1:1 Help Now