Hello, I have such a question, how to add text to a new file, to certain lines, for example, to the first line, etc.

using System; using System.IO; using System.Text; using System.Xml; using System.Text.RegularExpressions; public class FailR { public static void Main(string[] args) { string Path = @"C:\Users\Desktop\1\1.MST"; string Paths = @"C:\Users\Desktop\1\01.xml"; int Stroka = 9; string[] AllLines = File.ReadAllLines(Path); using (StreamWriter s = new StreamWriter(Paths)) for (int i = Stroka; i < AllLines.Length; i++) { s.WriteLine(AllLines[i].Replace(".", "255 ").Replace("X", "002 ").Replace("1", "001 ").Insert(0, "<Row><![CDATA[ ").Insert(202, " ]]></Row>")); } } } 

    1 answer 1

    No

    You can not add in the middle of a text file. Read the file line by line, output the information to another file, then delete the old file and rename the new one.

    Or read the entire file into memory, modify and write back (if the file is small).


    If you already read the entire document in memory, there are no problems. Check the index of the current line and modify, if necessary, before outputting.