VB6 到 VB.NET 的转换(语法:打印到 StreamWriter/Reader)? [英] VB6 to VB.NET conversion (Syntax : Print to StreamWriter/Reader)?
问题描述
以下代码是VB6应用的一部分&我目前正在转换为 VB.NET
Windows 服务.以 Open
开头的行给了我一个错误(我假设 VB.NET
不支持Open"语法).我尝试利用我拥有的所有 VB.NET 知识转换代码,但想知道理想/乐观的解决方案.
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 代码
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.......
我尝试过的:使用 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"类来更正Open"和Print #1"的错误?
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'?
此外,如果有人可以通过此代码行澄清他们试图做什么(老实说,我试图理解但不确定为什么他们将时间差乘以 24 * 60 * 60)dblTimeElapsed =(现在 - datTimeStart)* 24 * 60 * 60
?
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
?
推荐答案
&符号,&
,是 vb.net 的连接字符.虽然加号通常会起作用,但如果涉及数字,您可能会得到意想不到的结果.
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.
流必须被释放到释放的非托管资源.Using...End Using
块为我们解决了这个问题.
Streams must be disposed to released unmanaged resources. Using...End Using
blocks take care of this for us.
我将 filePath
设为类级变量,因为它在多个方法中使用.这也必须是 Shared
,因为它在 Shared
方法中使用.我更改了日期格式,以便在文件资源管理器中按时间顺序显示.
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.
阅读日志而不对其进行任何操作是没有意义的.我将 ReadLog
方法更改为 Function
.将字符串传递给它也是没有意义的.
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.
我相信 vb6 代码试图用 24 60 60 业务以秒为单位表示经过的时间.我给你举了一个例子,Form.Load
设置了 startTime
,然后在一段时间后点击一个按钮并计算已经过去的秒数.
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.
在表单类中...
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
这篇关于VB6 到 VB.NET 的转换(语法:打印到 StreamWriter/Reader)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!