VB6 到 VB.NET 的转换(语法:打印到 StreamWriter/Reader)?
VB6 to VB.NET conversion (Syntax : Print to StreamWriter/Reader)?
以下代码是 VB6 应用程序的一部分,我目前正在转换为 VB.NET
Windows 服务。以 Open
开头的行给我一个错误(我假设 'Open' 语法不支持 VB.NET
)。我尝试利用我拥有的所有 VB.NET 知识转换代码,但想知道 ideal/optimistic 解决方案。
VB6 代码
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.......
我试过的:创建了一个 'FileIO' class 如下 IO.StreamWriter
和 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
请告诉我我在上面的代码中犯的错误以及我应该如何使用 'FileIO' class 来更正 'Open' 和 [=38= 的错误]?
此外,如果有人可以通过此代码行澄清他们试图做什么(老实说,我试图理解但不确定他们为什么将时差乘以 24 * 60 * 60)dblTimeElapsed = (Now - datTimeStart) * 24 * 60 * 60
?
& 符号 &
是 vb.net 的连接字符。虽然加号通常会起作用,但如果涉及数字,您可能会得到意想不到的结果。
必须释放流以释放非托管资源。 Using...End Using
积木帮我们解决了这个问题。
我将filePath
设置为class级变量,因为它在不止一种方法中使用。这也必须是 Shared
因为它用在 Shared
方法中。我更改了日期格式,因此它会按时间顺序显示在文件资源管理器中。
阅读日志而不对其进行任何操作是没有意义的。我将 ReadLog
方法更改为 Function
。给它传递一个字符串也是没有意义的。
我相信 vb6 代码试图用 24 60 60 业务以秒为单位表示经过的时间。我给你举了一个例子 Form.Load
设置 startTime
然后在一段时间后按下按钮并计算已经过去的秒数。
形式为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
您的 class 看起来像这样...
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
以下代码是 VB6 应用程序的一部分,我目前正在转换为 VB.NET
Windows 服务。以 Open
开头的行给我一个错误(我假设 'Open' 语法不支持 VB.NET
)。我尝试利用我拥有的所有 VB.NET 知识转换代码,但想知道 ideal/optimistic 解决方案。
VB6 代码
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.......
我试过的:创建了一个 'FileIO' class 如下 IO.StreamWriter
和 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
请告诉我我在上面的代码中犯的错误以及我应该如何使用 'FileIO' class 来更正 'Open' 和 [=38= 的错误]?
此外,如果有人可以通过此代码行澄清他们试图做什么(老实说,我试图理解但不确定他们为什么将时差乘以 24 * 60 * 60)dblTimeElapsed = (Now - datTimeStart) * 24 * 60 * 60
?
& 符号 &
是 vb.net 的连接字符。虽然加号通常会起作用,但如果涉及数字,您可能会得到意想不到的结果。
必须释放流以释放非托管资源。 Using...End Using
积木帮我们解决了这个问题。
我将filePath
设置为class级变量,因为它在不止一种方法中使用。这也必须是 Shared
因为它用在 Shared
方法中。我更改了日期格式,因此它会按时间顺序显示在文件资源管理器中。
阅读日志而不对其进行任何操作是没有意义的。我将 ReadLog
方法更改为 Function
。给它传递一个字符串也是没有意义的。
我相信 vb6 代码试图用 24 60 60 业务以秒为单位表示经过的时间。我给你举了一个例子 Form.Load
设置 startTime
然后在一段时间后按下按钮并计算已经过去的秒数。
形式为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
您的 class 看起来像这样...
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