2015-12-29 243 views
-4

我有這樣的代碼:無法運行命令VB.NET

Try 
     cmd.Kill() 
    Catch ex As Exception 
    End Try 
    TextBox2.Clear() 
    If TextBox1.Text.Contains(" ") Then 
     psi = New ProcessStartInfo(TextBox1.Text.Split(" ")(0), TextBox1.Text.Split(" ")(1)) 
    Else 
     psi = New ProcessStartInfo("C:/Bukkit Runner/run.bat") 
    End If 
    Dim systemencoding As System.Text.Encoding 
    System.Text.Encoding.GetEncoding(Globalization.CultureInfo.CurrentUICulture.TextInfo.OEMCodePage) 
    With psi 
     .UseShellExecute = False 
     .RedirectStandardError = True 
     .RedirectStandardOutput = True 
     .RedirectStandardInput = True 
     .CreateNoWindow = True 
     .StandardOutputEncoding = systemencoding 
     .StandardErrorEncoding = systemencoding 
    End With 
    cmd = New Process With {.StartInfo = psi, .EnableRaisingEvents = True} 
    AddHandler cmd.ErrorDataReceived, AddressOf Async_Data_Received 
    AddHandler cmd.OutputDataReceived, AddressOf Async_Data_Received 
    cmd.Start() 
    cmd.BeginOutputReadLine() 
    cmd.BeginErrorReadLine() 

和文本框的命令

 If TextBox1.Text.Contains(" ") Then 
     psi = New ProcessStartInfo(TextBox1.Text.Split(" ")(0), TextBox1.Text.Split(" ")(1)) 
    Else 
     psi = New ProcessStartInfo(TextBox1.Text$) 
    End If 
    Dim systemencoding As System.Text.Encoding 
    System.Text.Encoding.GetEncoding(Globalization.CultureInfo.CurrentUICulture.TextInfo.OEMCodePage) 
    With psi 
     .UseShellExecute = False 
     .RedirectStandardError = True 
     .RedirectStandardOutput = True 
     .RedirectStandardInput = True 
     .CreateNoWindow = True 
     .StandardOutputEncoding = systemencoding 
     .StandardErrorEncoding = systemencoding 
    End With 
    cmd = New Process With {.StartInfo = psi, .EnableRaisingEvents = True} 
    AddHandler cmd.ErrorDataReceived, AddressOf Async_Data_Received 
    AddHandler cmd.OutputDataReceived, AddressOf Async_Data_Received 
    cmd.Start() 
    cmd.BeginOutputReadLine() 
    cmd.BeginErrorReadLine() 

,但我得到這個錯誤,如果我不停止在控制檯: http://prntscr.com/9jvf90

第一個開始命令工作正常。 http://prntscr.com/9jvfqe

我該如何在bat文件中運行命令?

回答

0

作爲Minecrafter,我可以清楚地看到您正在嘗試運行Bukkit服務器。

也許這會幫忙嗎?

Private Sub MyCode(sender As Object, e As EventArgs) Handles <stuff> 
Try 
Try 
     cmd.Kill() 
    Catch ex As Exception 
    End Try 
    TextBox2.Clear() 
Textbox_TextChanged(sender,e) 
Catch 
End 
End Try 
End Sub 
Private Sub Textbox_TextChanged(sender As Object, e As EventArgs) Handles Textbox1.textChanged, Textbox2.TextChanged 
Try 
     If TextBox1.Text.Contains(" ") Then 
     psi = New ProcessStartInfo(TextBox1.Text.Split(" ")(0), TextBox1.Text.Split(" ")(1)) 
    Else 
     psi = New ProcessStartInfo(TextBox1.Text$) 
    End If 
    Dim systemencoding As System.Text.Encoding 
    System.Text.Encoding.GetEncoding(Globalization.CultureInfo.CurrentUICulture.TextInfo.OEMCodePage) 
    With psi 
     .UseShellExecute = False 
     .RedirectStandardError = True 
     .RedirectStandardOutput = True 
     .RedirectStandardInput = True 
     .CreateNoWindow = True 
     .StandardOutputEncoding = systemencoding 
     .StandardErrorEncoding = systemencoding 
    End With 
    cmd = New Process With {.StartInfo = psi, .EnableRaisingEvents = True} 
    AddHandler cmd.ErrorDataReceived, AddressOf Async_Data_Received 
    AddHandler cmd.OutputDataReceived, AddressOf Async_Data_Received 
    cmd.Start() 
    cmd.BeginOutputReadLine() 
    cmd.BeginErrorReadLine() 
Catch 
End 
End Try 
End Sub 
+0

感謝您的回答。但使用此代碼運行時會崩潰。 :( 對不起,我的英語不好。 – Jordytjes