James
James

Reputation: 111

C# - Using File.WriteAllLines

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

private static void CompareOrig()
{

    File.WriteAllLines("process.txt",
    File.ReadAllLines("process2.txt").Except(File.ReadAllLines("process.txt")));

}

Added Info:

The problem is that when the lines are read from process2.txt, they are written to the process.txt file, hence overwriting all existing data in that file. Instead how could I append the output to process.txt? e.g.

File.AppendAllText("process.txt")

Upvotes: 4

Views: 8423

Answers (2)

gusmundo
gusmundo

Reputation: 9

A little old, but for anyone reading. If you only want to append to existing file use.

File.WriteLine("Text");

instead of

File.WriteAllLines("Text);

Upvotes: -3

Henk Holterman
Henk Holterman

Reputation: 273199

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

Yes

If so, how can this be avoided?

Use another filename for writing.

If you meant "will it be overwritten before it is completely read" then the answer is No and you have no problem.

But splitting this up with one or two variables would make it so much more readable. If it makes you doubt, so will it make the reader. Over and over again.

Upvotes: 5

Related Questions