The following code is a part of VB6 application & I'm currently converting to VB.NET
Windows Service. Line starts with Open
gives me an error(I assume the 'Open' syntax does not support with VB.NET
). I tried converting the code utilizing all the VB.NET knowledge I have but would like to know the ideal/optimistic solution.
VB6 Code
Private Sub Text1_GotFocus()
Me.lblCompanyName.Caption = ""
Me.lblCompanyName.Refresh
lngPosted = 0
lngSkipped = 0
lngClosed = 0
strMsg = Dir(strPath & "\WisysDataCollector_*.log", vbNormal)
Do While strMsg <> ""
On Error Resume Next
If strMsg < "WisysDataCollector_" & Format(DateAdd("m", -12, Now), "yyyyMM") Then
Kill(strPath & "\" & strMsg)
End If
On Error GoTo 0
strMsg = Dir()
Loop
datTimeStart = Now
Do
On Error Resume Next
Open strPath & "\WisysDataCollector_" & Format(Now, "yyyyMM") & ".log" For Append Lock Read Write As #1
lngST = Err.Number
strMsg = Err.Description
On Error GoTo 0
If lngST = 0 Then
Exit Do
End If
dblTimeElapsed = (Now - datTimeStart) * 24 * 60 * 60
If dblTimeElapsed > 20 Then
varResponse = vbCancel
If varResponse = vbCancel Then
strStatus = "Log file busy. Process aborted."
GoTo EXITFORM
End If
datTimeStart = Now
End If
Loop
Code continues.......
What I've tried : Created a 'FileIO' class as following with IO.StreamWriter
and IO.StreamReader
Public Class FileIO
Public Shared Sub WriteLog(strToWrite As String)
Dim filePath As String = AppDomain.CurrentDomain.BaseDirectory + "\WisysDataCollector_" + Format(Now, "MMddyy") + ".log"
Dim streamWr As IO.StreamWriter = Nothing
Try
streamWr = New IO.StreamWriter(filePath, True)
streamWr.Write(Now + " - " + strToWrite + vbNewLine)
streamWr.Flush()
streamWr.Close()
Catch ex As Exception
End Try
End Sub
Public Shared Sub ReadLog(strToWrite As String)
Dim filePath As String = AppDomain.CurrentDomain.BaseDirectory + "\WisysDataCollector_" + Format(Now, "MMddyy") + ".log"
Dim streamRd As IO.StreamReader = Nothing
Try
streamRd = New IO.StreamReader(filePath, True)
streamRd.Read()
streamRd.Close()
Catch ex As Exception
End Try
End Sub
End Class
Please let me know the errors I've made in the above code also how should I use the 'FileIO' class to correct the errors with 'Open' and 'Print #1'?
Also if someone can please clarify what were they trying to do by this code line(honestly I'm trying to understand but not sure why they've multiplied the time difference by 24 * 60 * 60) dblTimeElapsed = (Now - datTimeStart) * 24 * 60 * 60
?
The ampersand, &
, is the concatenation character for vb.net. Although the plus sign will usually work, if numbers are involved you could get unexpected results.
Streams must be disposed to released unmanaged resources. Using...End Using
blocks take care of this for us.
I made filePath
a class level variable because it is used in more than one method. This must also be Shared
because it is used in Shared
methods. I changed the format of date so it will appear chronologically in File Explorer.
It makes no sense to read the log and do nothing with it. I changed the ReadLog
method to a Function
. It also makes no sense to pass a string to it.
I believe the vb6 code was trying to express elapsed time in seconds with the 24 60 60 business. I gave you an example of that with the Form.Load
setting the startTime
and then hitting a button some time later and calculating the seconds that had passed.
In the form class...
Private StartTime As DateTime
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
StartTime = Now
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FileIO.WriteLog(TextBox1.Text)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox2.Text = FileIO.ReadLog
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim elapsedTime As TimeSpan = Now - StartTime
Dim TotalSeconds = elapsedTime.TotalSeconds
MessageBox.Show($"The elapsed time since the program started is {TotalSeconds}")
End Sub
Your class would look like this...
Public Class FileIO
Private Shared filePath As String = AppDomain.CurrentDomain.BaseDirectory & "\WisysDataCollector_" & Format(Now, "yyyyMMdd") & ".log"
Public Shared Sub WriteLog(strToWrite As String)
Using sw = File.AppendText(filePath)
sw.WriteLine(strToWrite)
End Using
End Sub
Public Shared Function ReadLog() As String
If File.Exists(filePath) Then
Return File.ReadAllText(filePath)
Else
Return ""
End If
End Function
End Class