I am writing a small console app which have to overwrite a txt file with another txt file, however the final executes 3 times, I think it is because the IO writing process is slower than the IO output process. Anyone can help me how can I execute the loop only once?
Here is the code:
while (confirm != 'x') {
Console.WriteLine(
"Do you want to copy the archive to test2.txt? (y)es or e(x)it");
confirm = (char)Console.Read();
if (confirm == 's') {
File.Copy("C:\\FMUArquivos\\test.txt",
"C:\\FMUArquivos\\test2.txt", true);
Console.WriteLine("\nok\n");
}
Console.WriteLine("\ncounter: " + counter);
counter += 1;
}
If you hit
y<enter>then this will give you the 3-character sequence"y"+<cr>+<lf>and will produce three iterations and therefore the counter will be increased by 3. UseReadLineinstead.