代码之家  ›  专栏  ›  技术社区  ›  Skitzafreak

开闭操作等价vb6/vb.net

  •  0
  • Skitzafreak  · 技术社区  · 8 年前

    我正在努力将VB6项目的部分转换成VB,还有一些代码段我有问题,因为我在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 如果还没有打开或创建就可以工作?

    对于大多数其他的转换烦恼,我已经与移植到这一个最困扰我,所以任何帮助都是感激的。谢谢你的时间。

    2 回复  |  直到 8 年前
        1
  •  3
  •   Joel Coehoorn    8 年前

    这将修复错误,并更新许多代码,以使用更典型的现代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 对于vb.net现在 明确建议不要使用匈牙利类型前缀。

        2
  •  0
  •   the_lotus    8 年前

    如果您只需要写一行,就可以使用构建方法来完成所有的工作。

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

    更多帮助: 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