I have a code that:
- reads a text file,
- replaces all "spaces" with ";",
- removes the repetition of the character ";"
- and saves a new file with all changes.
But instead of writing all the rows in the file, it writes only the last row.
static void Main(string[] args)
{
RegexOptions options = RegexOptions.None;
Regex regex = new Regex("[ ]{2,}", options);
string FilePath = @"C:\Users\User\Desktop\download1.txt";
string OutputhFilePath = @"C:\Users\User\Desktop\download2.txt";
List<string> lines = new List<string>();
lines = File.ReadAllLines(FilePath).ToList();
foreach (string line in lines)
{
string line1 = line.Replace(' ', ';');
line1 = regex.Replace(line, ";");
Console.WriteLine(line1);
File.WriteAllText(OutputhFilePath, line1);
}
Console.ReadLine();
}
Can someone help me with these? Thanks in advance!