【问题标题】:How to open a message box after a shell command如何在 shell 命令后打开消息框
【发布时间】:2020-12-05 07:24:37
【问题描述】:

我正在构建一个用于在远程计算机上重新启动 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

任何帮助将不胜感激!

【问题讨论】:

  • 您应该首先使用Process.Start 而不是Shell,但在这种情况下您绝对需要。它返回一个Process 对象,允许您通过重定向的输出流获取输出。是时候对这个主题进行一些研究了。

标签: vb.net process


【解决方案1】:
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")

【讨论】:

    猜你喜欢
    • 2017-03-01
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多