The lines in the .txt file are:

stroka1 stroka2 

etc.

I need to insert a space at the beginning of each line, that is, make a пробелstroka1 .

 string[] file = File.ReadAllLines(@"C:\\\\123.txt"); foreach (var lines in file) { string str = lines.Insert(0, " "); Console.WriteLine(lines); File.WriteAllText(@"C:\\\\123.txt", str); } 

Why is a space not inserted before each line?

  • Sorry, Tode. Should not have deleted the question because of a frivolous comment. And in my thoughts I didn’t laugh at you. But who is this garbage did not suffer? And then many capable and talented for life remains. Just by itself, the ridiculous element of the atmosphere is craft. And I am sure that someone would answer the question. Even I, if he worked in eklipse. Many people have something wrong with IDE. In general, more sense of humor :) - Sergey

4 answers 4

  1. In the console, you output the initial line ( lines ), and not the modified ( str ). If in the console you want to see the modified line, then you need to output it exactly.
  2. At each iteration of the loop, you overwrite the contents of the file, so that as a result it will only have the last line changed. In general, it is better to write the entire result to the file once, rather than open and close the file at each iteration of the loop.
  3. When using @ suffices to write @"C:\\123.txt" .
  4. The names of the file and lines variables are unsuccessful, since the first is an array of all lines in the file (that is, the name lines more appropriate for it), and the second is a specific line in the file (that is, line ).

The result is:

 string fileName = @"C:\\123.txt"; string[] lines = File.ReadAllLines(fileName); for (int i = 0; i < lines.Length; i++) { lines[i] = lines[i].Insert(0, " "); Console.WriteLine(lines[i]); } File.WriteAllLines(fileName, lines); 

    First, the @ symbol escapes the entire line, and therefore it is not necessary to separate the slashes separately. It should stay like this:

     string[] file = File.ReadAllLines(@"C:\\123.txt"); 

    And secondly, and in fact, the strings in C # are immutable. You inserted a space and saved the result in the variable str (while the original line remained in lines , without a space), and the original line from the variable variable was written to the file

    • In the code presented in the question, the variable str is written to the file. - Regent
     string[] file = File.ReadAllLines(@"C:\123.txt"); var result = string.Empty; for (var i = 0; i < file.Length; i++) { var line = file[i]; result += " " + line; if (i != file.Length - 1) result += Environment.NewLine; } Console.WriteLine(result); File.WriteAllText(@"C:\123.txt", result); 
       static string fileName = @"E:\test1.txt"; static string symbolForInsert = " "; static void Main(string[] args) { string[] lines = File.ReadAllLines(fileName); StringBuilder sb = new StringBuilder(); foreach (var line in lines) { string str = line.Insert(0, symbolForInsert); Console.WriteLine(str); sb.AppendLine(str); } File.WriteAllText(fileName, sb.ToString()); Console.ReadKey(); }