简体   繁体   中英

Open/Close Operation Equivalence VB6 / VB.NET

I am working on converting parts of a VB6 Project to VB.Net and there are some code segments I am having issues with because I can't seem to find alternatives for the VB6 code in VB.Net. Here is the code block in question right now:

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

So the lines I am having issue with are:

Open strPathName For Append As #intFile
Write #intFile
Close #intFile

I understand I should probably be using the StreamWriter object in place of these, but what throws me off is the Error section. If an error is thrown and it goes to the 100 mark, how would Close #intFile work if it hasn't been opened or created yet?

For most of the other conversion annoyances I've had with porting this over this one has been confusing me the most, so any help is appreciated. Thanks for your time.

This fixes the errors, and also updates a lot of the code to use styles and APIs more typical for modern VB.Net. For this to work as-is, make sure there is an Imports System.IO directive at the top of the file.

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

One thing worth pointing out here is the style guidelines published by Microsoft for VB.Net now explicitly recommend against hungarian type-prefixes.

If you just have one line to write to, you can use the build-in method that does all the work for you.

Dim inputString As String = "This is a test string."
My.Computer.FileSystem.WriteAllText(
  "C://testfile.txt", inputString, True)

More help here: https://docs.microsoft.com/en-us/dotnet/visual-basic/developing-apps/programming/drives-directories-files/how-to-append-to-text-files?view=netframework-4.7.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM