簡體   English   中英

查找PID VB.net

[英]Find PID VB.net

我正在嘗試在游戲中制作服務器啟動器。 我必須使用不同的配置啟動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進行處理?

不確定確切的操作方式...但是如果現有的游戲實例正在運行,則會殺死它,然后再重新啟動它:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM