C# - 使用 File.WriteAllLines [英] C# - Using File.WriteAllLines

查看:34
本文介绍了C# - 使用 File.WriteAllLines的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的方法会覆盖输出文件(process.txt)吗?

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")));

}

添加信息:

问题在于,当从 process2.txt 读取这些行时,它们会被写入 process.txt 文件,因此会覆盖该文件中的所有现有数据.相反,我如何将输出附加到 process.txt?例如

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")

推荐答案

下面的方法会覆盖输出文件(process.txt)吗?

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

是的

如果是这样,如何避免这种情况?

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.

这篇关于C# - 使用 File.WriteAllLines的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆