Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I am creating an application in c# for replacing a string in a file.
The input to the application are the file that I want to replace and a xml file that contains the actual line and its replacement line.I am reading the xml file and if my file contains the line which is present in the xml,it gets replaced;
I am replacing the first occurrence of the string.
private void Replace_Click(object sender, EventArgs e)
        {
            string dir = @"D:\Output";
            string path = XmltextBox.Text;
            string txtpath = Filetextbox.Text;
           
            if (!Directory.Exists(dir))  // if it doesn't exist, create a directory
                Directory.CreateDirectory(dir);
            string file = Path.Combine(dir, "output.txt");
          
            System.Xml.XmlDocument docxml = new System.Xml.XmlDocument();
            XmlNodeList xmlnode;
            int i = 0;
 
            string str = null;
           
           
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            docxml.Load(fs);
            xmlnode = docxml.GetElementsByTagName("rule");
            for (i = 0; i <= xmlnode.Count - 1; i++)
            {
                 if (i == 1)//Background color change
                {
                    string line;
                    FileStream tempFile = new FileStream(despath, FileMode.Open);
                    StreamReader Templine = new StreamReader(tempFile16);
 
                    while ((line = Templine.ReadLine()) != null)
                    {
                        string linevalue = xmlnode[1].ChildNodes.Item(1).InnerText.Trim();
 
                        if (line.Contains(linevalue))
                        {
                          
                            string actual=line;
                                    
                            string replace16 ="currentClip.stopDrag();"
 
                            string replaceVariable = ReplaceFirstOccurence(text,actual, replace);
 
                            FileStream filewrite = new FileStream(despath, FileMode.Create);
                            StreamWriter filew = new StreamWriter(filewrite);
                            
                            filew.Write(replaceVariable);
 
                            filew.Close();
                            filewrite.Close();
 
                        }
                    }
                    Templine.Close();
                    tempFile.Close();
                }
               }
}
                
For Eg: I want to replace the line "stopDrag();" with "currentClip.stopDrag();"
If my file contains "stopDrag();" only once then it is working fine.But if my file contains "stopDrag();" twice then it is getting replaced as "currentClip.currentClip.stopDrag();"
If there are 3 occurrences then it is getting replaced 3 times.
How to avoid such replacements?
Posted 5-Oct-12 21:11pm
Edited 5-Oct-12 21:36pm
v2
Comments
Kenneth Haugland at 6-Oct-12 3:15am
   
I think we need to see your attempt. If I understood you correctly you have displayed your file in a text box and would like to move the lines around?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this
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();
}
D4dilip.wordpress.com
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,448
1 Tadit Dash 270
2 OriginalGriff 223
3 Sergey Alexandrovich Kryukov 215
4 Peter Leow 180
0 Sergey Alexandrovich Kryukov 9,440
1 OriginalGriff 5,618
2 Peter Leow 4,280
3 Maciej Los 3,540
4 Abhinav S 3,363


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 6 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid