3

C#から外部Windowsコンソールアプリケーションを自動化する必要があります。アプリケーションは基本的に外部デバイスへのインターフェースです。アプリケーションを呼び出すと、認証を求められます。つまり、「Enter password:」のようなプロンプトでパスワードを入力するように求められます。現在、インタラクティブなパスワードプロンプトなしで実行するようにこのアプリケーションを構成する方法はありません。

したがって、プロンプトが表示されるたびにパスワードを送信し、外部デバイスで実行されて出力を取得するコマンドを起動することで、C#から同じことを自動化したいと思います。私はプロセスクラスについて知っていて、この目的のためにパイプを使用できるようないくつかのポインターを持っています(わからない?)。

私はこれからの助け/方向性を探す前にこの種の自動化を扱ったことがないので。

前もって感謝します。

4

1 に答える 1

5

これを行う方法は、Redirection メンバーを使用することです。たとえば、ProcessStartInfo.RedirectStandardInput プロパティです。

     Process myProcess = new Process();

     myProcess.StartInfo.FileName = "someconsoleapp.exe";
     myProcess.StartInfo.UseShellExecute = false;
     myProcess.StartInfo.RedirectStandardInput = true;
     myProcess.StartInfo.RedirectStandardOutput = true;
     myProcess.StartInfo.ErrorDialog = false;

     myProcess.Start();

     StreamWriter stdInputWriter  = myProcess.StandardInput;
     StreamReader stdOutputReader  = myProcess.StandardOutput;

     stdInputWriter.WriteLine(password);

     var op = stdOutputReader.ReadLine();

     // close this - sending EOF to the console application - hopefully well written
     // to handle this properly.
     stdInputWriter.Close();


     // Wait for the process to finish.
     myProcess.WaitForExit();
     myProcess.Close();
于 2013-01-03T09:51:07.880 に答える