4

I have 'Applications.PrevInstance' in VB 6 code that I am trying to upgrade to .NET using VS 2008. Apparently this code is no longer valid. Does anyone have any ideas about upgraded solution? TIA

4

2 回答 2

8

See here:

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
于 2010-09-08T14:56:55.870 回答
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
于 2016-11-04T10:56:06.223 回答