4

I want to run an exe file by my c# code. The exe file is a console application written in c#.

The console application performs some actions which includes writing content in database and writing some files to directory.

The console application (exe file) expects some inputs from user. Like it first asks , 'Do you want to reset database ?' y for yes and n for no. again if user makes a choice then application again asks , 'do you want to reset files ?' y for yes and n for no. If user makes some choice the console application starts to get executed.

Now I want to run this exe console application by my c# code. I am trying like this

        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();
        }

I want to know how can I provide user inputs to the console application by my c# code?

Please help me out in this. Thanks in advance.

3
  • I'm not sure you want what you are asking for. Obviously, the straightforward way to provide arguments is by setting the ProcessStartInfo.Arguments property... however, in your description you say that the program "asks" the user for input sequentially. That is not the same thing. It may just be a problem of terminology, but "arguments" provided to a program are input provided once, and only once, when the program is first executed. Commented Apr 16, 2013 at 6:16
  • "One by one"...? You mean, you'll pass an arg, then wait, then pass another? Or do you mean you'll pass a bunch of args at once? Commented Apr 16, 2013 at 6:17
  • Actually I mean to say that how can i provide user inputs (which console application) expects by c# code? Commented Apr 16, 2013 at 8:07

3 Answers 3

2

You can redirect input and output streams from your exe file.
See redirectstandardoutput and redirectstandardinput for examples.

For reading:

 // 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();

For writing:

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

 StreamWriter myStreamWriter = myProcess.StandardInput;
 myStreamWriter.WriteLine("y");
 ...
 myStreamWriter.Close();
Sign up to request clarification or add additional context in comments.

Comments

0

ProcessStartInfo has a constructor that you can pass arguments to:

public ProcessStartInfo(string fileName, string arguments);

Alternatively, you can set it on it's property:

ProcessStartInfo p = new ProcessStartInfo();
p.Arguments = "some argument";

Comments

0

Here is a sample of how to pass arguments to the *.exe file:

                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();

6 Comments

This example does not provide any useful arguments... only a file name (where did file come from anyway?). It also introduces a lot of undeclared variables. It is also calling a compiler executable... Where did you copy this code from? I mean, I get that it is an example.. but it's going to confuse the OP even further.
Yeah, it just includes a lot of unnecessary stuff (as well as variables which are undeclared and irrelevant) and is likely to confuse a beginner. This entire example essentially boils down to "set the ProcessStartInfo.Arguments property".
I just removed a lot of unnecessary junk. I didn't copy the code from anywhere - it's old code I wrote a long time ago.
There you go :D. I didn't mean to say that you were cribbing someone's code, I just figured you copied existing code from somewhere. I don't think it's what the OP is asking for though, he is using the term "command line arguments" incorrectly.
@EdS. No worries :) I'll wait for further info from the OP, so I can understand exactly what they're looking for.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.