保存文件 C# - 不覆盖?

发布于 2024-11-11 06:46:04 字数 115 浏览 4 评论 0原文

我每 60 秒将数据保存到一个文件,我让它每 60 秒保存一次,但它会删除以前添加的数据。有没有一种方法可以将其添加到文件而不是覆盖它。并且无需使用保存对话框,因为它是在后台完成的。

非常感谢任何反馈

I am saving data to a file every 60 secs, I have it saving every 60 secs ok but it deletes the previous data added. Is there a way in which it would add to the file rather than overwrite it. And without using save dialog as it is done in the background.

Any feedback is greatly appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

老子叫无熙 2024-11-18 06:46:04

看来以追加模式打开文件可以解决您的问题。
http://msdn.microsoft.com/en-us/library/3zc0w663.aspx

It seems opening the File in Append mode will solve your problem.
http://msdn.microsoft.com/en-us/library/3zc0w663.aspx

分开我的手 2024-11-18 06:46:04

尝试,

using (StreamWriter writer = new StreamWriter(@"C:\somefile.txt", true))
            {
                writer.WriteLine("Hello line..");
            }

Try,

using (StreamWriter writer = new StreamWriter(@"C:\somefile.txt", true))
            {
                writer.WriteLine("Hello line..");
            }
沧桑㈠ 2024-11-18 06:46:04

FileStream 构造函数中指定 FileMode.Append

Specify FileMode.Append in the FileStream constructor.

半衾梦 2024-11-18 06:46:04

使用FileAppendText方法。

StreamWriter sw = File.AppendText("D:\\Text.txt");
sw.WriteLine("Hello");
sw.WriteLine("World");
sw.WriteLine("Test");
sw.Flush();
sw.Close();

Use File class AppendText method.

StreamWriter sw = File.AppendText("D:\\Text.txt");
sw.WriteLine("Hello");
sw.WriteLine("World");
sw.WriteLine("Test");
sw.Flush();
sw.Close();
长伴 2024-11-18 06:46:04

您可以在构造函数中将 StreamWriterappend 一起使用为 true

请参阅此处

You can use StreamWriter with append as true in the constructor.

See here.

泛泛之交 2024-11-18 06:46:04

好的,我收到你的问题你有一个文件并且你想在其中保存新数据而不是替换它

你可以使用流阅读器打开其内容。并将要插入到文件中的数据附加到文件中
使用 writer 不会每次都保存文件,只需在该文件上附加数据,这样当您只是在该文件上附加某些内容时,它就不会覆盖该文件。

OK as i am getting your question You are having a File And you want to save new data on that instead of replacing it

You can just open its content using stream reader.and append that data you want to insert on the file
using writer don't save file each time just append data on that file so when you just appending something on that it will not overwrite that file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文