3

C# コードで exe ファイルを実行したい。exe ファイルは、c# で記述されたコンソール アプリケーションです。

コンソール アプリケーションは、データベースへのコンテンツの書き込みやディレクトリへのファイルの書き込みなど、いくつかのアクションを実行します。

コンソール アプリケーション (exe ファイル) は、ユーザーからの入力を予期します。最初に「データベースをリセットしますか?」と尋ねます。はいの場合は y、いいえの場合は n です。ユーザーが選択すると、アプリケーションは再び「ファイルをリセットしますか?」と尋ねます。はいの場合は y、いいえの場合は n です。ユーザーが何らかの選択を行うと、コンソール アプリケーションの実行が開始されます。

ここで、この exe コンソール アプリケーションを C# コードで実行したいと考えています。私はこのようにしようとしています

        string strExePath = "exe path";
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.FileName = strExePath;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;

        using (Process exeProcess = Process.Start(startInfo))
        {
            exeProcess.WaitForExit();
        }

C# コードでコンソール アプリケーションにユーザー入力を提供する方法を教えてください。

これで私を助けてください。前もって感謝します。

4

3 に答える 3

2

exe ファイルから入力ストリームと出力ストリームをリダイレクトできます。例については、 redirectstandardoutput およびredirectstandardinput
を 参照してください。

読むために:

 // Start the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "Write500Lines.exe";
 p.Start();
 // Do not wait for the child process to exit before
 // reading to the end of its redirected stream.
 // p.WaitForExit();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();

書き込み用:

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

 StreamWriter myStreamWriter = myProcess.StandardInput;
 myStreamWriter.WriteLine("y");
 ...
 myStreamWriter.Close();
于 2013-04-16T06:17:42.507 に答える
0

ProcessStartInfo引数を渡すことができるコンストラクターがあります。

public ProcessStartInfo(string fileName, string arguments);

または、そのプロパティで設定できます。

ProcessStartInfo p = new ProcessStartInfo();
p.Arguments = "some argument";
于 2013-04-16T06:15:10.817 に答える
0

*.exe ファイルに引数を渡す方法のサンプルを次に示します。

                Process p = new Process();

                // Redirect the error stream of the child process.
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.FileName = @"\filepath.exe";
                p.StartInfo.Arguments = "{insert arguments here}";

                p.Start();


                error += (p.StandardError.ReadToEnd());
                p.WaitForExit();
于 2013-04-16T06:15:20.677 に答える