vb.net - 如何在 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
任何帮助将不胜感激!
解决方案
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")
推荐阅读
- ios - 在分段控件中向 tableview 添加数据的问题
- python - 如何解决硒中的“AttributeError:'NoneType'对象没有属性'send_keys'”错误
- testing - 如何正确使用黄瓜?
- php - 如何查询和求和具有相同名称的数据,但仍然获取名称
- c++ - 如何声明priority_queue的自定义比较函数
- c++ - 无法静态链接 SDL2
- bash - 如何将bash变量作为字符串而不是数组传递给scala文件
- python - 为什么我的函数返回“函数 ___ at 0x7f37c058d378”?
- javascript - 检测元素何时跨越某个类的任何 div
- python-3.x - python中的PooledOLS