Abhishek Gahlout
Abhishek Gahlout

Reputation: 3462

Passing arguments one by one one in console exe by c# code

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.

Upvotes: 4

Views: 3548

Answers (3)

jay_t55
jay_t55

Reputation: 11652

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

Upvotes: 0

Tomasz
Tomasz

Reputation: 415

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

Upvotes: 2

c0D3l0g1c
c0D3l0g1c

Reputation: 3164

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";

Upvotes: 0

Related Questions