나는 또한 모든 응용 프로그램이 stderr과 stdout을 적절하게 사용하지 않기 때문에 "콘솔 응용 프로그램"에서 볼 수있는 정보가 예상 한 위치에 전달되지 않을 수도 있습니다.
stderr를 더 이상 리디렉션하지 않으면 구문과 관련되어 있고 응용 프로그램이 예외를 던지는지 확인할 수도 있습니다.
나는 수업을 마무리하여, 그에 추가 할
startInfo.RedirectStandardError = true;
, 당신은이 단지 실행하고 반환 ... 당신은 자동으로 응용 프로그램을 자동화하려는 경우하지, cmd를 쉘과 상호 작용할 수 있습니다 가능성이 당신이 원하는 접근 방식 ...
방금 VB에서 사람을위한이 샘플을 썼는데, 난 년 VB에서 시작하지 않았기 때문에 구문 어쩌면 거친 것입니다,하지만 당신은 그것의 요점을 얻을 수 있어야합니다 C#에서 쉽게 복제 할 수 있습니다. 이 초안 방법 형 접근하지 나는 물고기 샘플 사람을 가르치고, 생산 준비를 부를 것이다 코드의 조각이었다
#Region " Imports "
Imports System.Threading
Imports System.ComponentModel
#End Region
Namespace Common
Public Class CmdShell
#Region " Variables "
Private WithEvents ShellProcess As Process
#End Region
#Region " Events "
''' <summary>
''' Event indicating an asyc read of the command process's StdOut pipe has occured.
''' </summary>
Public Event DataReceived As EventHandler(Of CmdShellDataReceivedEventArgs)
#End Region
#Region " Public Methods "
Public Sub New()
ThreadPool.QueueUserWorkItem(AddressOf ShellLoop, Nothing)
Do Until Not ShellProcess Is Nothing : Loop
End Sub
''' <param name="Value">String value to write to the StdIn pipe of the command process, (CRLF not required).</param>
Public Sub Write(ByVal value As String)
ShellProcess.StandardInput.WriteLine(value)
End Sub
#End Region
#Region " Private Methods "
Private Sub ShellLoop(ByVal state As Object)
Try
Dim SI As New ProcessStartInfo("cmd.exe")
With SI
.Arguments = "/k"
.RedirectStandardInput = True
.RedirectStandardOutput = True
.RedirectStandardError = True
.UseShellExecute = False
.CreateNoWindow = True
.WorkingDirectory = Environ("windir")
End With
Try
ShellProcess = Process.Start(SI)
With ShellProcess
.BeginOutputReadLine()
.BeginErrorReadLine()
.WaitForExit()
End With
Catch ex As Exception
With ex
Trace.WriteLine(.Message)
Trace.WriteLine(.Source)
Trace.WriteLine(.StackTrace)
End With
End Try
Catch ex As Exception
With ex
Trace.WriteLine(.Message)
Trace.WriteLine(.Source)
Trace.WriteLine(.StackTrace)
End With
End Try
End Sub
Private Sub ShellProcess_ErrorDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles ShellProcess.ErrorDataReceived
If Not e.Data Is Nothing Then RaiseEvent DataReceived(Me, New CmdShellDataReceivedEventArgs(e.Data))
End Sub
Private Sub ShellProcess_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles ShellProcess.OutputDataReceived
If Not e.Data Is Nothing Then RaiseEvent DataReceived(Me, New CmdShellDataReceivedEventArgs(e.Data & Environment.NewLine))
End Sub
#End Region
End Class
<EditorBrowsable(EditorBrowsableState.Never)> _
Public Class CmdShellDataReceivedEventArgs : Inherits EventArgs
Private _Value As String
Public Sub New(ByVal value As String)
_Value = value
End Sub
Public ReadOnly Property Value() As String
Get
Return _Value
End Get
End Property
End Class
End Namespace
그냥 할) 확인에는 함정이 없었다, 내가 나서서 및 C#에서이 더러운 짓
public class cmdShell
{
private Process shellProcess;
public delegate void onDataHandler(cmdShell sender, string e);
public event onDataHandler onData;
public cmdShell()
{
try
{
shellProcess = new Process();
ProcessStartInfo si = new ProcessStartInfo("cmd.exe");
si.Arguments = "/k";
si.RedirectStandardInput = true;
si.RedirectStandardOutput = true;
si.RedirectStandardError = true;
si.UseShellExecute = false;
si.CreateNoWindow = true;
si.WorkingDirectory = Environment.GetEnvironmentVariable("windir");
shellProcess.StartInfo = si;
shellProcess.OutputDataReceived += shellProcess_OutputDataReceived;
shellProcess.ErrorDataReceived += shellProcess_ErrorDataReceived;
shellProcess.Start();
shellProcess.BeginErrorReadLine();
shellProcess.BeginOutputReadLine();
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
void shellProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
doOnData(e.Data);
}
void shellProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
doOnData(e.Data);
}
private void doOnData(string data)
{
if (onData != null) onData(this, data);
}
public void write(string data)
{
try
{
shellProcess.StandardInput.WriteLine(data);
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
}
이제 장소에이
cmdShell test = new cmdShell();
test.onData += test_onData;
test.write("ping 127.0.0.1");
처럼 사용
void test_onData(cmdShell sender, string e)
{
Trace.WriteLine(e);
}
완전한 대화식 cmd 프로세스를 사용하여 비동기 데이터를 읽고받을 수 있습니다. 창
C:\Windows>ping 127.0.0.1
Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Ping statistics for 127.0.0.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
커피, 잠
출력 ... 당신이 정말로 날짜를 원하는 경우
롤 ...
cmdShell test = new cmdShell();
test.onData += test_onData;
test.write("date");
, 당신은 실제로 아직 코드를 사용하지 않은 경우에, 그런데 출력
C:\Windows>echo %date%
Wed 10/08/2014
냅니다 출력이 방법을
C:\Windows>date
The current date is: Wed 10/08/2014
또는
cmdShell test = new cmdShell();
test.onData += test_onData;
test.write("echo %date%");
를 산출 데이터 비동기, 의미 제공 으로으로 프로그램 출력이 전달되므로 100ping, traceroute 등과 같이 시간이 걸리는 프로세스를 실행하면 결과가 완료 될 때까지 기다릴 필요가 없습니다.
또한 중에 응용 프로그램 에 명령을 전달할 수 있습니다. 예를 들어 취소, 반응 및 구문 변경, 또는 첫 번째 실행 결과를 기반으로 비슷한 다른 명령 실행 등이 가능합니다.
기본적으로 cmd 창에 입력하는 것처럼 처리 할 수 있습니다. 피드백을 받으면 적절하게 스레드 된 양식 (다른 스레드의 Windows 컨트롤을 업데이트 할 때주의하십시오)에서 전체 생각을 감싸고 에뮬레이트 된 cmd 프롬프트.
그래서 '내가 왜 문제가 생겼는지'무슨 문제가 있습니까? "작동하지 않습니다"는 문제에 대한 설명이 아닙니다. 그것은 무엇을 하는가? 추락? 오류가 있습니까? 특이점으로 접어 들었습니까? 전혀하지 마라. 당신이 * 그것을 할 것으로 기대 했습니까? –
당신의 문제는 아마도'date'만으로 새로운 날짜를 입력하라는 명령을 내게됩니다 (명령 행에서 시도해보십시오). 'date/T'를 시도하면 날짜를 출력 할 것이고 아무런 입력을 요구하지 않습니다. 또는 표준 입력에'enter '를 쓸 수도 있습니다. –