找到PID VB.net

时间:2015-10-03 19:09:55

标签: vb.net process pid restart process.start

我正在尝试在游戏中制作服务器启动器。我必须使用不同的配置启动GameServer.exe GameServer.exe GameServer1.cfg
GameServer.exe GameServer2.cfg
GameServer.exe GameServer3.cfg等。

这是我必须启动每个.exe和.cfg的当前代码

Private Sub GS1_Click(sender As Object, e As EventArgs) Handles GS1.Click
    Dim path As String = Directory.GetCurrentDirectory()
    Dim gameservercfg As String = GameServer1.Text
    Dim newpath As String = path + ("\")
    System.Diagnostics.Process.Start(newpath + "GameServer.exe ", gameservercfg)
End Sub

这是我的代码,按进程名称重新启动它。

Private Sub GSRES_Click(sender As Object, e As EventArgs) Handles GSRES.Click
    Dim path As String = Directory.GetCurrentDirectory()
    Dim gameservercfg As String = GameServer.Text
    Dim newpath As String = path + ("\")
    Dim p() As Process

    p = Process.GetProcessesByName("GameServer")
    If p.Count > 0 Then
        ' Process is running
        Process.GetProcessesByName("GameServer")(0).Kill()
        System.Diagnostics.Process.Start(newpath + "GameServer.exe ", gameservercfg)
    Else
        ' Process is not running
        MsgBox("GameServer is not running!")
    End If
End Sub

使用该代码,它会重新启动随机游戏服务器 问题是,我如何通过ProcessID代替Process name?

1 个答案:

答案 0 :(得分:0)

不确定你在这之后究竟是什么......但是如果现有的游戏实例在再次启动之前运行它会杀死它:

Private Games As New Dictionary(Of String, Process)

Private Sub GS1_Click(sender As Object, e As EventArgs) Handles GS1.Click
    Dim gameservercfg As String = GameServer1.Text
    Dim Key As String = gameservercfg.ToUpper

    If Games.ContainsKey(Key) Then
        If Not Games(Key).HasExited Then
            Games(Key).Kill()
        End If
        Games.Remove(Key)
    End If

    Dim psi As New ProcessStartInfo
    psi.FileName = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "GameServer.exe")
    psi.WorkingDirectory = System.IO.Path.GetDirectoryName(psi.FileName)
    psi.Arguments = gameservercfg
    Games.Add(Key, System.Diagnostics.Process.Start(psi))
End Sub