如何解决VB6 到 VB.NET 的转换语法:打印到 StreamWriter/Reader?
以下代码是 VB6 应用程序的一部分,我目前正在转换为 VB.NET
Windows 服务。以 Open
开头的行给了我一个错误(我假设“打开”语法不支持 VB.NET
)。我尝试利用我拥有的所有 VB.NET 知识转换代码,但想知道理想/乐观的解决方案。
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”的错误?
另外,如果有人可以通过此代码行澄清他们试图做什么(老实说,我试图理解但不确定为什么他们将时差乘以 24 * 60 * 60)dblTimeElapsed = (Now - datTimeStart) * 24 * 60 * 60
?
解决方法
与号 //Author - Chandan Kumar
//Thanks to Rajandran of www.marketcalls.in for original supertrend script
//Multi time frame supertrend - 6 timeframe supertrends in one chart
//@version=4
study("Multitimeframe Supertrend",overlay = true)
Factor=input(4,minval=1,maxval = 100)
Pd=input(10,maxval = 100)
Up=hl2-(Factor*atr(Pd))
Dn=hl2+(Factor*atr(Pd))
float TrendUp=na
float TrendDown=na
TrendUp:=close[1]>TrendUp[1]? max(Up,TrendUp[1]) : Up
TrendDown:=close[1]<TrendDown[1]? min(Dn,TrendDown[1]) : Dn
Trend() =>
float Trend=na
Trend := close > TrendDown[1] ? 1: close< TrendUp[1]? -1: nz(Trend[1],1)
Tsl() =>
Tsl = Trend()==1? TrendUp: TrendDown
tickerid = syminfo.ticker
Trend15 = security(tickerid,"15",Trend())
Trend30 = security(tickerid,"30",Trend())
Trend60 = security(tickerid,"60",Trend())
Trend120 = security(tickerid,"120",Trend())
Trend240 = security(tickerid,"240",Trend())
TrendD = security(tickerid,"D",Trend())
TrendW = security(tickerid,"W",Trend())
Tsl15 = security(tickerid,Tsl())
Tsl30 = security(tickerid,Tsl())
Tsl60 = security(tickerid,Tsl())
Tsl120 = security(tickerid,Tsl())
Tsl240 = security(tickerid,Tsl())
TslD = security(tickerid,Tsl())
TslW = security(tickerid,Tsl())
//linecolor = Trend == 1 ? green : red
linecolor15 = Trend15 == 1 ? color.green : color.red
linecolor30 = Trend30 == 1 ? color.green : color.red
linecolor60 = Trend60 == 1 ? color.green : color.red
linecolor120 = Trend120 == 1 ? color.green : color.red
linecolor240 = Trend240 == 1 ? color.green : color.red
linecolorD = TrendD == 1 ? color.green : color.red
linecolorW = TrendW == 1 ? color.green : color.red
plot(Tsl15,color = linecolor15,style = plot.style_line,linewidth = 1,title = "SuperTrend15")
plot(Tsl30,color = linecolor30,style = plot.style_linebr,title = "SuperTrend30")
plot(Tsl60,color = linecolor60,style = plot.style_circles,title = "SuperTrend60")
plot(Tsl120,color = linecolor120,style = plot.style_cross,title = "SuperTrend120")
plot(Tsl240,color = linecolor240,title = "SuperTrend240")
plot(TslD,color = linecolorD,linewidth = 2,title = "SuperTrendD")
plot(TslW,color = linecolorW,title = "SuperTrendW")
//plotshape(cross(close,Tsl) and close>Tsl,"Up Arrow",shape.triangleup,location.belowbar,green,0)
//plotshape(cross(Tsl,close) and close<Tsl,"Down Arrow",shape.triangledown,location.abovebar,red,0)
//plot(Trend==1 and Trend[1]==-1,color = linecolor,style = circles,linewidth = 3,title="Trend")
//plotarrow(Trend == 1 and Trend[1] == -1 ? Trend : na,title="Up Entry Arrow",colorup=lime,maxheight=60,minheight=50,transp=0)
//plotarrow(Trend == -1 and Trend[1] == 1 ? Trend : na,title="Down Entry Arrow",colordown=red,transp=0)
是 vb.net 的连接字符。虽然加号通常会起作用,但如果涉及数字,您可能会得到意想不到的结果。
流必须被释放到释放的非托管资源。 &
块为我们处理这个问题。
我将 Using...End Using
设为类级别变量,因为它在多个方法中使用。这也必须是 filePath
,因为它在 Shared
方法中使用。我更改了日期格式,以便在文件资源管理器中按时间顺序显示。
阅读日志而不对其进行任何操作是没有意义的。我将 Shared
方法更改为 ReadLog
。将字符串传递给它也是没有意义的。
我相信 vb6 代码试图用 24 60 60 业务以秒为单位表示经过的时间。我举了一个例子,其中 Function
设置了 Form.Load
,然后在一段时间后按下按钮并计算经过的秒数。
在表单类中...
startTime
你的班级看起来像这样...
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。