如何将VB 6.0中的Application.PrevInstance转换为VB.NET?

时间:2010-09-08 14:54:50

标签: vb6 vb6-migration

我在VB 6代码中有'Applications.PrevInstance',我试图使用VS 2008升级到.NET。显然这段代码不再有效。有没有人对升级解决方案有任何想法? TIA

2 个答案:

答案 0 :(得分:7)

见这里:

http://www.knowdotnet.com/articles/previnstance.html

Public Sub Main()
   If PrevInstance() Then Exit Sub

   ' continue with your application
   UserName = Environ("UserName")
   ComputerName = Environ("COMPUTERNAME")

End Sub

Function PrevInstance() As Boolean
  If UBound(Diagnostics.Process.GetProcessesByName _
     (Diagnostics.Process.GetCurrentProcess.ProcessName)) _
     > 0 Then
     Return True
  Else
     Return False
  End If
End Function

答案 1 :(得分:0)

Function PrevInstance() As Boolean
    If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
        PrevInstance = True
    Else
        UserName = Environ("UserName")
        Computername = Environ("COMPUTERNAME")
        PrevInstance = False
    End If
    Dim i, n As Integer, RepForm As String
    For i = My.Application.OpenForms.Count - 1 To 1 Step -1
        RepForm = My.Application.OpenForms.Item(i).Name
        For n = My.Application.OpenForms.Count - 1 To 1 Step -1
            If My.Application.OpenForms.Item(n).Name = My.Application.OpenForms.Item(i).Name And n > i Then
                My.Application.OpenForms(i).Close()
                PrevInstance = True
                Exit Function
            End If
        Next n
    Next i
End Function