Click here to Skip to main content
15,886,806 members

String replace in a file

DhanashreeRamya asked:

Open original thread
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?
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900