MAC MAC - 6 months ago 55
Vb.net Question

Using StreamWriter in VB.NET won't write to text file

I have read these sources (first Stack Overflow question, second Stack Overflow question, third Stack Overflow question, fourth Stack Overflow question and Microsoft streamwriter) and it did not solve the problem.

I have this code below.

FIRST

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


SECOND

' 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


I am trying to make a log file and as you can see, there is no much difference between the two, besides the variables. The FIRST code actually outputs or writes to
.log
but the SECOND code wouldn't. I needed both codes to write different logs and the second code executes first and executes more times than the first code.

What can the problem be?

I tried to add this in the second code:

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


And I even tried the
.flush()
and
.close()
and the other way which is not:

Using
End Using


But nothing works.

Answer

Here's is your problem:

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

Especially this part:

propertyCd = "_"

That is a comparison and returns either True or False.

In general I would use String.Format for your file name and Path.Combine to build the path.

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

Also, use the debugger to check the values of variables at runtime.

Comments