我需要一种快速而肮脏的方法来附加到 vb.net 中的文本文件

发布于 2024-07-08 17:54:47 字数 159 浏览 8 评论 0原文

我有一个非常小的独立 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 技术交流群。

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

发布评论

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

评论(5

嗳卜坏 2024-07-15 17:54:47

IO.File.AppendAllText(@"Y:\our\File\ Name.here", "这里是您的日志消息")

IO.File.AppendAllText(@"Y:\our\File\Name.here", "your log message here")

美煞众生 2024-07-15 17:54:47
Private Const LOG_FILE As String = "C:\Your\Log.file"

Private Sub AppendMessageToLog(ByVal message As String)
    If Not File.Exists(LOG_FILE) Then
        File.Create(LOG_FILE)
    End If

    Using writer As StreamWriter = File.AppendText(LOG_FILE)
        writer.WriteLine(message)
    End Using
End Sub
Private Const LOG_FILE As String = "C:\Your\Log.file"

Private Sub AppendMessageToLog(ByVal message As String)
    If Not File.Exists(LOG_FILE) Then
        File.Create(LOG_FILE)
    End If

    Using writer As StreamWriter = File.AppendText(LOG_FILE)
        writer.WriteLine(message)
    End Using
End Sub
梅窗月明清似水 2024-07-15 17:54:47

这是用 C# 编写的,但更改为 VB 应该很简单:

    void logMessage(string message)
    {
        string logFileName = "log.file";

        File.AppendAllText(logFileName,message);
    }

已编辑,因为 Joel 的解决方案比我的简单得多 =)

This is in C#, but should be trivial to change to VB:

    void logMessage(string message)
    {
        string logFileName = "log.file";

        File.AppendAllText(logFileName,message);
    }

Edited because Joel's solution was much simpler than mine =)

荒人说梦 2024-07-15 17:54:47

这篇 MSDN 文章如何:将文本写入文件应该执行此操作它。

This MSDN article, How to: Write Text to a File should do it.

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