我在Visual Basic中创建了一个应用程序,它打开cmd并通过VPN将文件传输到Android接收器。它工作正常,但我如何从cmd获得响应以检查传输是否成功?
示例代码
Public Class Form1
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
Shell("cmd.exe /k" + "adb push C:\Users\user\Desktop\Newfolder\1.png /sdcard/test")
End Sub
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
Shell("adb connect " + TextBox1.Text)
btnSend.Enabled = True
btnConnect.Enabled = False
End Sub
结束班
答案 0 :(得分:0)
我假设你想获得adb命令的返回码或std输出。无论哪种方式,您都需要启动自己的进程而不是使用Shell命令,因为:
进程可以在终止时返回退出代码。但是,您无法使用Shell来检索此退出代码,因为如果等待终止,Shell将返回零,并且还因为该进程在与Shell不同的对象中运行。 From http://msdn.microsoft.com/en-us/library/xe736fyk%28v=vs.90%29.aspx
该链接将向您展示如何设置返回退出代码的进程。相关代码是
Dim procID As Integer
Dim newProc As Diagnostics.Process
newProc = Diagnostics.Process.Start("C:\WINDOWS\NOTEPAD.EXE")
procID = newProc.Id
newProc.WaitForExit()
Dim procEC As Integer = -1
If newProc.HasExited Then
procEC = newProc.ExitCode
End If
MsgBox("Process with ID " & CStr(ProcID) & _
" terminated with exit code " & CStr(procEC))
如果你不想要返回代码,而是程序的标准输出,那么根据http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process.standardoutput?cs-save-lang=1&cs-lang=vb#code-snippet-4 你可以通过这段代码片段来做到这一点:
Imports System
Imports System.IO
Imports System.Diagnostics
Class IORedirExample
Public Shared Sub Main()
Dim args() As String = Environment.GetCommandLineArgs()
If args.Length > 1
' This is the code for the spawned process'
Console.WriteLine("Hello from the redirected process!")
Else
' This is the code for the base process '
Dim myProcess As New Process()
' Start a new instance of this program but specify the spawned version. '
Dim myProcessStartInfo As New ProcessStartInfo(args(0), "spawn")
myProcessStartInfo.UseShellExecute = False
myProcessStartInfo.RedirectStandardOutput = True
myProcess.StartInfo = myProcessStartInfo
myProcess.Start()
Dim myStreamReader As StreamReader = myProcess.StandardOutput
' Read the standard output of the spawned process. '
Dim myString As String = myStreamReader.ReadLine()
Console.WriteLine(myString)
myProcess.WaitForExit()
myProcess.Close()
End If
End Sub
End Class
当你自己尝试这个时,请记住你必须包括
myProcessStartInfo.UseShellExecute = False
也行。