C # - Using File.WriteAllLines

Will the method described below overwrite the output file (process.txt)?

private static void CompareOrig() { File.WriteAllLines("process.txt", File.ReadAllLines("process2.txt").Except(File.ReadAllLines("process.txt"))); } 

Information added:

The problem is that when the lines are read from the process2.txt file, they are written to the process.txt file and, therefore, overwrite all existing data in this file. Instead, how can I add output to process.txt? eg.

 File.AppendAllText("process.txt") 
+4
source share
2 answers

Will the method described below overwrite the output file (process.txt)?

Yes

If so, how can this be avoided?

Use a different file name for the record.

If you meant β€œwhether it will be overwritten before it is completely read,” then the answer will be β€œNo,” and you have no problem.

But dividing this into one or two variables would make it more readable. If it makes you doubt, it will make the reader. Again and again.

+4
source

A bit old, but for anyone reading. If you only want to add to your existing file usage.

 File.WriteLine("Text"); 

instead

 File.WriteAllLines("Text); 
-1
source

All Articles