我需要知道,如果你能帮助我,如何在vb中插入命令,那么它们在cmd中运行,我得到输出。
我需要做" net localgroup Administradores a58465 / add"并获取错误消息(如果有)。
解决方案:`Dim myProcess As Process = New Process Dim s As String myProcess.StartInfo.FileName =" c:\ windows \ system32 \ cmd.exe" myProcess.StartInfo.UseShellExecute = False myProcess.StartInfo.CreateNoWindow = True myProcess.StartInfo.RedirectStandardInput = True myProcess.StartInfo.RedirectStandardOutput = True myProcess.StartInfo.RedirectStandardError = True myProcess.Start()
Dim sIn As System.IO.StreamWriter = myProcess.StandardInput
Dim sOut As System.IO.StreamReader = myProcess.StandardOutput
Dim sErr As System.IO.StreamReader = myProcess.StandardError
'sIn.AutoFlush = True
sIn.Write("cls" & System.Environment.NewLine)
sIn.Write("net user" & System.Environment.NewLine)
sIn.Write("exit" & System.Environment.NewLine)
s = sOut.ReadToEnd()
If Not myProcess.HasExited Then
myProcess.Kill()
End If
LB1.Text = s
LB1.Visible = True
sIn.Close()
sOut.Close()
sErr.Close()
myProcess.Close()`
答案 0 :(得分:1)
查看Process.Start。 http://msdn.microsoft.com/en-us/library/0w4h05yb(v=vs.110).aspx
还要查找ProcessStartInfo类,它将为您提供有关如何启动外部进程的选项。
可以通过ProcessStartInfo为您的程序提供控制台输入和输出。