首页 > 解决方案 > 如何在 shell 命令后打开消息框

问题描述

我正在构建一个用于在远程计算机上重新启动 VNC 服务的应用程序。

我有多个用于多台计算机的复选框;我使用以下命令来完成这项工作,并且工作正常。

但我需要得到一个messagebox显示命令完成且没有错误,或者如果发生任何错误,如访问被拒绝,也显示了这一点。

    If CheckBox2.CheckState = CheckState.Checked Then
        Shell("psservice.exe \\192.168.1.48 -u .\user -p 123 restart WinVNC4", AppWinStyle.Hide)
    End If

    If CheckBox3.CheckState = CheckState.Checked Then
        Shell("psservice.exe \\192.168.1.15 -u .\user -p 123 restart WinVNC4", AppWinStyle.Hide)
    End If

任何帮助将不胜感激!

标签: vb.netprocess

解决方案


Public Sub HandleService(strIP As String)

    ' Create the psservice.exe process object
    Dim p As New Process()

    ' Set it to run hidden from user, so it appears smoother.
    With p.StartInfo
        .RedirectStandardOutput = True
        .RedirectStandardError = True
        .FileName = "psservice.exe"
        .Arguments = String.Format("{0} {1} {2} {3}", "\\" &  strIP, "-u .\user", "-p 123", "restart WinVNC4")
        .UseShellExecute = False
        .CreateNoWindow = True
    End With

    p.Start()

    Dim myStreamReader As StreamReader = p.StandardError
    
    ' Read the standard error of psservice.exe and write it to console (or do your messagebox thing, etc.).
    Console.WriteLine(myStreamReader.ReadLine())
    
    ' Wait for psservice.exe to finish before we handle it's output (Sync method, manding the thread won't continue until this one is finished. Use .exited if wanting to do Async)
    ' Also note that you can add milliseconds to this if wanted. i.e. .WaitForExit(1000)
    p.WaitForExit()

End Sub

像这样称呼它:

HandleService("192.168.1.48")

推荐阅读