[英]Using StreamWriter in VB.NET won't write to text file
我已经阅读了这些资料( 第一个Stack Overflow问题 , 第二个Stack Overflow问题 , 第三个Stack Overflow问题 , 第四个Stack Overflow问题和Microsoft 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
但是什么都行不通。
这是您的问题:
Dim strFile As String = "Files\" + propertyCd = "_" + filename + "_" + Date.Now.ToString("yyyyMMdd") + ".log"
特别是这部分:
propertyCd = "_"
那是一个比较,并返回True
或False
。
通常,我将使用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)
另外,使用调试器在运行时检查变量的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.