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
solved VB6 to VB.NET conversion (Syntax : Print to StreamWriter/Reader)? [closed]