如何在 C# 中现有文本文件的顶部追加行

发布于 2024-09-28 22:30:13 字数 143 浏览 5 评论 0原文

你能在这里帮我吗... 我想通过应用程序(c#-.Net2005)创建一个文本文件,并且需要为每个成功的事务更新它...我创建了文本文件并且更新也成功,但新行被添加为最后一行...但是我希望新添加的行应该是文本文件中的最顶层行...您能告诉我如何在 c# 中执行此操作吗?

Can you please help me here...
I wants to create a text file through application (c#-.Net2005)and need to update it for every sucessful transaction...I created text file and also updation is sucessful but new line is gets added as last line...BUT I want newly added line should be a TOPMOST line in the text file...Can you please tell me how can I do this in c#.

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

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

发布评论

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

评论(2

自由如风 2024-10-05 22:30:13

如果它是在线更新的,并且只有一行在文件开头留有空间,并使用以下命令写入此位置。

FileStream.write( buffer, 0, space_reserved_for_line);
//be aware that this will overwrite what has been written there

如果您想保留某种日志文件,请将该行写入一个新文件并附加其余部分其他文件。这是非常昂贵的(性能方面)。除非您有充分的理由,否则您很可能不想这样做。

没有可接受的方法来写入最顶层条目是最新条目的日志文件。

马里奥

If it is on line you update and only one line leave space at the beginning of the file and write to this position using

FileStream.write( buffer, 0, space_reserved_for_line);
//be aware that this will overwrite what has been written there

If you want to keep some kind of a log file, write the line to a new file and appen the rest of the other file. This is very expensive (performancewise). Unless you have very good reasons, you most probably do not want to do this.

There is no acceptable way to write a log-file where the top-most entry ist the most recent one.

hth

Mario

天赋异禀 2024-10-05 22:30:13
string topLine = "....";
System.IO.File.WriteAllText("filename.blah",
                              topLine + System.Environment.NewLine + 
                                   System.IO.File.ReadAllText("filename.blah") );

我个人认为这个要求值得质疑。

string topLine = "....";
System.IO.File.WriteAllText("filename.blah",
                              topLine + System.Environment.NewLine + 
                                   System.IO.File.ReadAllText("filename.blah") );

Personally I think this requirement should be questioned.

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