我需要一种快速而肮脏的方法来附加到 vb.net 中的文本文件
我有一个非常小的独立 vb.net 应用程序,可以自动运行。 有时它会遇到一个错误情况,我想记录并继续处理。 但是,这对于存储在系统的主日志中来说太小了 - 我真的只想在文本文件中附加一行。
在 .net 下将一行文本附加到文件(如果不存在则创建该文件)的最轻松的方法是什么?
I've got a very small standalone vb.net app that gets run automatically. Every now and then it hits an error condition that I want to log and then keep processing. But, this is far too minor a thing to store in the system's main log - I really just want to append a line to a text file.
What's the least stress way to append a line of text to a file (and have it create the file if it's not there) under .net?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
IO.File.AppendAllText(@"Y:\our\File\ Name.here", "这里是您的日志消息")
IO.File.AppendAllText(@"Y:\our\File\Name.here", "your log message here")
在 VB.NET 中,My.Computer.FileSystem.WriteAllText< /a> 就可以了。 如果您不喜欢“我的”命名空间,System.IO.File.AppendAllText< /a> 也可以。
In VB.NET, My.Computer.FileSystem.WriteAllText will do the trick. If you don't like the My namespace, System.IO.File.AppendAllText works as well.
这是用 C# 编写的,但更改为 VB 应该很简单:
已编辑,因为 Joel 的解决方案比我的简单得多 =)
This is in C#, but should be trivial to change to VB:
Edited because Joel's solution was much simpler than mine =)
这篇 MSDN 文章如何:将文本写入文件应该执行此操作它。
This MSDN article, How to: Write Text to a File should do it.