Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine("ipconfig"); string line =p.StandardOutput.ReadToEnd(); p.WaitForExit();//等待程序执行完退出进程
p.Close();//关闭进程
这段代码在线程里面
string line =p.StandardOutput.ReadToEnd();走到这的时候就不走了。这是为什么啊?
我换下面这种方式
StreamReader reader = p.StandardOutput;
string line = reader.ReadLine();
while (!reader.EndOfStream)
{
line += "\r\n" + reader.ReadLine();
}
一行一样读的时候,也是一样,while没执行完就不走了。但是我看了line里面的内容,和CMD里面的一样完整。
请问高手,这是何故啊?
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine("ipconfig"); string line =p.StandardOutput.ReadToEnd(); p.WaitForExit();//等待程序执行完退出进程
p.Close();//关闭进程
这段代码在线程里面
string line =p.StandardOutput.ReadToEnd();走到这的时候就不走了。这是为什么啊?
我换下面这种方式
StreamReader reader = p.StandardOutput;
string line = reader.ReadLine();
while (!reader.EndOfStream)
{
line += "\r\n" + reader.ReadLine();
}
一行一样读的时候,也是一样,while没执行完就不走了。但是我看了line里面的内容,和CMD里面的一样完整。
请问高手,这是何故啊?
Imports System.Threading
''' <summary>
''' 此类实现将程序实现重定位输入和输出
''' </summary>
''' <res></res>
Public Class Shell_exe
#Region "声明"
Implements IDisposable Dim CmdPcs As New Process
Dim Displayer As TextBoxBase = Nothing
Dim CmdSW As StreamReader = Nothing
Dim file_name As String = String.Empty
Protected disposed As Boolean = False
Private Delegate Sub Write(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
#End Region
#Region "构造和析构"
Public Sub New(ByRef filename As String)
file_name = filename
End Sub
Public Sub New(ByRef filename As String, ByRef sender As TextBoxBase)
file_name = filename
Displayer = sender
End Sub Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposed Then
If disposing Then
CmdPcs = Nothing
CmdSW = Nothing
file_name = Nothing
End If
Displayer = Nothing
End If
Me.disposed = True
End Sub Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub Protected Overrides Sub Finalize()
Dispose(False)
MyBase.Finalize()
End Sub#End Region
#Region "方法"
Public Function Shell(ByRef sender As Object, ByRef sw As StreamWriter) As Boolean
Displayer = sender
Shell(sw)
End Function Public Sub Shell(ByRef sw As StreamWriter)
Try
CmdPcs = New Process
With CmdPcs.StartInfo
.FileName = file_name
.UseShellExecute = False
.CreateNoWindow = True
.RedirectStandardInput = True
.RedirectStandardOutput = True
.RedirectStandardError = True
End With
CmdPcs.Start()
sw = CmdPcs.StandardInput
CmdPcs.BeginErrorReadLine()
CmdPcs.BeginOutputReadLine()
AddHandler CmdPcs.ErrorDataReceived, AddressOf Output
AddHandler CmdPcs.OutputDataReceived, AddressOf Output
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub Public Sub Kill()
Try
CmdPcs.Kill()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Output(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
Try
If Not String.IsNullOrEmpty(e.Data) Then
If Displayer.InvokeRequired Then
Displayer.Invoke(New Write(AddressOf Output), sender, e)
Else
Displayer.AppendText(e.Data & vbCrLf)
End If
End If
Catch ex As Exception
End Try
End Sub
#End Region
End Class
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "ipconfig.exe";
p.Start();
p.WaitForExit();
string line = p.StandardOutput.ReadToEnd();
这样跳过cmd.exe就可以了,但是有个问题,如果我执行带参数的命令,如:
rasdial.exe DSL 0000 0000 /phonebook:rasphone.pbk
这怎么做?
CmdPcs.FileName 只给 ipconfig.exe应该可以,带上参数我这里是不行了。
p.StartInfo.Arguments = "0000 0000 a1b2c3 /phonebook:rasphone.pbk";
上面的方式是错的。用现在给出的这个方式就好了