Open/Close 运算等效 VB6 / VB.NET
Open/Close Operation Equivalence VB6 / VB.NET
我正在努力将 VB6 项目的一部分转换为 VB.Net,但我遇到了一些代码段问题,因为我似乎无法在 VB.Net 中找到 VB6 代码的替代方案].这是现在有问题的代码块:
Public Sub ProcessError(ByVal strModule As String, ByVal strProcedure As String, _
ByVal strDescription As String, ByVal bLogError As Boolean, _
ByVal bShowError As Boolean, Optional ByVal strMsg As String)
On Error GoTo 100
Dim intFile As Integer: Dim strPathName As String
strPathName = AddBackSlash(gsLogPath) & gsErrLogName
If bLogError = True Then
If GetFileSize(strPathName) > gcuMaxLogFileSize Then
Call CopyFile(strPathName, strPathName & ".bak")
Call DeleteFile(strPathName)
End If
intFile = FreeFile
Open strPathName For Append As #intFile
Write #intFile, Format(Now, "MMM-DD-YYYY HH:MM:SS AMPM"), strModule, strProcedure, strDescription)
Close #intFile
End If
If bShowError Then
Call Prompt("Error occurred in " & strModule & vbCrLf & "Error Description :" & strDescription, 1, vbRed)
End If
Exit Sub
100:
Close #intFile
End Sub
所以我遇到的问题是:
Open strPathName For Append As #intFile
Write #intFile
Close #intFile
我知道我可能应该使用 StreamWriter
对象来代替这些,但让我失望的是错误部分。如果抛出错误并到达 100
标记,如果尚未打开或创建 Close #intFile
将如何工作?
对于大多数其他转换烦恼,我在将它移植到这个上时遇到了很多麻烦,这让我最困惑,所以任何帮助都将不胜感激。感谢您的宝贵时间。
如果您只有一行要写入,您可以使用内置方法为您完成所有工作。
Dim inputString As String = "This is a test string."
My.Computer.FileSystem.WriteAllText(
"C://testfile.txt", inputString, True)
这修复了错误,还更新了大量代码以使用现代 VB.Net 更典型的样式和 API。为了按原样工作,请确保文件顶部有一个 Imports System.IO
指令。
Public Sub ProcessError(ByVal ModuleName As String, ByVal ProcedureName As String, _
ByVal Description As String, ByVal LogError As Boolean, _
ByVal ShowError As Boolean, Optional ByVal Message As String)
If LogError Then
Dim logFile As New FileInfo(Path.Combine(gsLogPath, gsErrLogName))
If logFile.Length > gcuMaxLogFileSize Then
logFile.MoveTo(logFile.FullName & ".bak")
End If
Try
File.AppendAllText(PathName, String.Format("{0:d},""{1}"",""{2}"",""{3}""", DateTime.Now, ModuleName, ProcedureName, Description))
Catch
End Try
End If
If ShowError Then
MsgBox(String.Format("Error occurred in {0}{1}Error Description:{2}", ModuleName, vbCrLf, Description))
End If
End Sub
这里值得指出的一件事是 style guidelines published by Microsoft for VB.Net 现在 明确建议不要使用匈牙利语类型前缀。
我正在努力将 VB6 项目的一部分转换为 VB.Net,但我遇到了一些代码段问题,因为我似乎无法在 VB.Net 中找到 VB6 代码的替代方案].这是现在有问题的代码块:
Public Sub ProcessError(ByVal strModule As String, ByVal strProcedure As String, _
ByVal strDescription As String, ByVal bLogError As Boolean, _
ByVal bShowError As Boolean, Optional ByVal strMsg As String)
On Error GoTo 100
Dim intFile As Integer: Dim strPathName As String
strPathName = AddBackSlash(gsLogPath) & gsErrLogName
If bLogError = True Then
If GetFileSize(strPathName) > gcuMaxLogFileSize Then
Call CopyFile(strPathName, strPathName & ".bak")
Call DeleteFile(strPathName)
End If
intFile = FreeFile
Open strPathName For Append As #intFile
Write #intFile, Format(Now, "MMM-DD-YYYY HH:MM:SS AMPM"), strModule, strProcedure, strDescription)
Close #intFile
End If
If bShowError Then
Call Prompt("Error occurred in " & strModule & vbCrLf & "Error Description :" & strDescription, 1, vbRed)
End If
Exit Sub
100:
Close #intFile
End Sub
所以我遇到的问题是:
Open strPathName For Append As #intFile
Write #intFile
Close #intFile
我知道我可能应该使用 StreamWriter
对象来代替这些,但让我失望的是错误部分。如果抛出错误并到达 100
标记,如果尚未打开或创建 Close #intFile
将如何工作?
对于大多数其他转换烦恼,我在将它移植到这个上时遇到了很多麻烦,这让我最困惑,所以任何帮助都将不胜感激。感谢您的宝贵时间。
如果您只有一行要写入,您可以使用内置方法为您完成所有工作。
Dim inputString As String = "This is a test string."
My.Computer.FileSystem.WriteAllText(
"C://testfile.txt", inputString, True)
这修复了错误,还更新了大量代码以使用现代 VB.Net 更典型的样式和 API。为了按原样工作,请确保文件顶部有一个 Imports System.IO
指令。
Public Sub ProcessError(ByVal ModuleName As String, ByVal ProcedureName As String, _
ByVal Description As String, ByVal LogError As Boolean, _
ByVal ShowError As Boolean, Optional ByVal Message As String)
If LogError Then
Dim logFile As New FileInfo(Path.Combine(gsLogPath, gsErrLogName))
If logFile.Length > gcuMaxLogFileSize Then
logFile.MoveTo(logFile.FullName & ".bak")
End If
Try
File.AppendAllText(PathName, String.Format("{0:d},""{1}"",""{2}"",""{3}""", DateTime.Now, ModuleName, ProcedureName, Description))
Catch
End Try
End If
If ShowError Then
MsgBox(String.Format("Error occurred in {0}{1}Error Description:{2}", ModuleName, vbCrLf, Description))
End If
End Sub
这里值得指出的一件事是 style guidelines published by Microsoft for VB.Net 现在 明确建议不要使用匈牙利语类型前缀。