【问题标题】:Using StreamWriter in VB.NET won't write to text file在 VB.NET 中使用 StreamWriter 不会写入文本文件
【发布时间】:2015-10-13 10:02:22
【问题描述】:

我已经阅读了这些资源(first Stack Overflow questionsecond Stack Overflow questionthird Stack Overflow questionfourth Stack Overflow questionMicrosoft streamwriter),但并没有解决问题。

我在下面有这段代码。

第一

Private Sub WritePropertyLog(ByVal folderPath As String, ByVal file_name As String, ByVal property_cd As String, _
                ByVal property_name As String, ByVal status As String)

    Using w As StreamWriter = File.AppendText("Files\" + "DS_IKO_" + Date.Today.ToString("yyyyMMdd") + ".log")
        PropertyLog(folderPath, file_name, property_cd, property_name, status, w)
    End Using

End Sub

Private Sub PropertyLog(ByVal folderPath As String, ByVal file_name As String, ByVal property_cd As String, _
                ByVal property_name As String, ByVal status As String, ByVal w As TextWriter)

    w.Write(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + ", ")
    w.WriteLine("{0}, {1}, {2}, {3}, {4}", folderPath, file_name, property_cd, property_name, status)

End Sub

第二次

' Logging each record ----------
Private Sub WriteRecordLog(ByVal row_number As String, ByVal columnB As String, ByVal columnC As String, _
                ByVal columnD As String, ByVal sekisan_cd As String, ByVal propertyCd As String, ByVal filename As String)

    Dim strFile As String = "Files\" + propertyCd = "_" + filename + "_" + Date.Now.ToString("yyyyMMdd") + ".log"
    Using writer As StreamWriter = File.AppendText(strFile)
        RecordLog(row_number, columnB, columnC, columnD, sekisan_cd, writer)
    End Using

End Sub

Private Sub RecordLog(ByVal row_number As String, ByVal columnB As String, ByVal columnC As String, _
                ByVal columnD As String, ByVal sekisan_cd As String, ByVal w As TextWriter)

    w.WriteLine("{0}, {1}, {2}, {3}, {4}", row_number, columnB, columnC, columnD, sekisan_cd)

End Sub

我正在尝试创建一个日志文件,如您所见,除了变量之外,两者之间没有太大区别。 FIRST 代码实际上输出或写入.log,但 SECOND 代码不会。我需要这两个代码来编写不同的日志,第二个代码首先执行并且比第一个代码执行的次数更多。

可能是什么问题?

我尝试在第二个代码中添加这个:

    Dim fs As FileStream = Nothing
    If (Not File.Exists(strFile)) Then
        fs = File.Create(strFile)
    End If

我什至尝试了.flush().close() 以及另一种方法:

Using
End Using

但没有任何效果。

【问题讨论】:

  • 你有异常吗?你用过调试器吗?
  • 一般情况下,使用Path.Combine构建路径。它使它更容易和更具可读性。
  • 您确定您的文件路径吗?您是否在正确的文件夹中查找?您正在使用 relative 路径,这意味着在调试期间您正在写入 bin/debug/Files
  • propertyCd = "_" 看起来不对 - 这是故意的吗?
  • 使用String.Format创建文件名而不是连接字符串以避免拼写错误

标签: .net vb.net logging streamwriter


【解决方案1】:

这是你的问题:

Dim strFile As String = "Files\" + propertyCd = "_" + filename + "_" + Date.Now.ToString("yyyyMMdd") + ".log"

尤其是这部分:

propertyCd = "_"

这是一个比较,返回 TrueFalse

一般来说,我会使用String.Format 作为您的文件名并使用Path.Combine 来构建路径。

Dim file = String.Format("{0}_{1}_{2}.log", 
    propertyCd, filename, Date.Now.ToString("yyyyMMdd")) 
Dim strFile As String = Path.Combine("Files", file)

另外,使用调试器在运行时检查变量的值。

【讨论】:

  • 我不敢相信就是这样......我花了整整两个小时试图解决它。谢谢!
猜你喜欢
  • 2014-05-16
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 2011-01-20
  • 1970-01-01
  • 1970-01-01
  • 2014-02-22
  • 2018-08-24
相关资源
最近更新 更多