2017-05-19 13 views
0

私はprocess.OutputDataReceivedを購読しようとしていました。結果が表示されない(イベントは出力で発生しない)私はMsdnの例に従った。しかし、process.BeginOutputReadLine();は私に例外を与えます。私が実行しようとしているプロセスは単純なバッチファイルコマンドですwmic product where "Name like '%%Microsoft Visual C++ %%'" get Name, Versionちょっとだけ、process.Exitedイベントはうまくいきます。ここProcess.Start() 'System.ComponentModel.Win32Exception'がSystem.dllで発生しました

コード

void DoSomething(object sendingProcess, DataReceivedEventArgs outLine) 
    { 
     Application.Current.Dispatcher.Invoke(new Action(() => { textBox.Text = outLine.Data; })); 
    } 
    public MainWindow() 
    { 
     InitializeComponent(); 

     string batPath = @"..\..\WMIC batch\"; 

     var process = new Process(); 

     process.StartInfo.WorkingDirectory = batPath; 
     process.StartInfo.FileName = "Redistributable_Packages_Check.bat"; 
     process.StartInfo.UseShellExecute = false; 
     process.StartInfo.RedirectStandardOutput = true; 

     process.EnableRaisingEvents = true; 
     process.Exited += new EventHandler(Process_Exited); 

     process.OutputDataReceived +=new DataReceivedEventHandler(DoSomething); 

     process.StartInfo.RedirectStandardInput = true; 
     process.Start(); 

     StreamWriter sortStreamWriter = process.StandardInput; 
     process.BeginOutputReadLine(); 

     //textBox.Text = "Initialised"; 



    } 

    private void Process_Exited(object sender, EventArgs e) 
    { 
     Application.Current.Dispatcher.Invoke(new Action(() => { textBox.Text = "Process has exited"; })); 
    } 

例外の詳細{ "指定されたファイルが見つかりません"}行

process.StartInfo.UseShellExecute = false; 
    process.StartInfo.RedirectStandardOutput = true; 
    process.StartInfo.RedirectStandardInput = true; 

を省略

P.Sバッチファイルをトリガです。だから私はそれがファイルを見つけることができると信じています。

+0

cmdでコマンドを実行しようとするとどうなりますか? – C0d1ngJammer

+1

バッチファイルへの絶対パスを指定してください:Process.Start( "c:\ yourfolder \ WMIC batch \ Redistributable_Packages_Check.bat"); – mm8

+0

@ C0d1ngJammer cmdでコマンドを実行すると、システムにインストールされているMicrosoftパッケージが表示されます。 Microsoft Visual C++ 2008 Redistributable - x86 9.0.30729.4974 9.0.30729.4974, Microsoft Visual C++ 2013 x86-x64コンパイラ12.0.21005,Microsoft Visual C++ 2015 x64デバッグランタイム - 14.0.24215 14.0.24215,Microsoft Visual C++ 2010 x64再頒布可能パッケージ - 10.0.40219 10.0.40219、 – Phil15

答えて

0

あなたのような小さな例を作成しましたが、その理由はのフルパスprocess.StartInfo.FileNameに指定する必要があるという理由がコメントに記載されています。 WorkingDirectoryを設定するだけでは不十分です。

だから例えば:

process.StartInfo.WorkingDirectory = batPath; 
process.StartInfo.FileName = System.IO.Path.Combine(batPath, "Redistributable_Packages_Check.bat"); 

WorkingDirectoryはそれに関連し新しいプロセスで使用される相対的なファイル名のパスを指定します。しかし、FileNameは、あなたの現在のプロセスの作業ディレクトリに相対的です。

関連する問題