kosa
kosa

Reputation: 51

Passing arguments to running process in C#

I've some troubles with running processes and passing args to them. I know how to run process with some args

ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", "/c something");  
Process p = Process.Start(psi)

The problem is that after script is executed process is terminated. That's why there is "/c"

But I'm running multiple scripts and I would like to run them in one process ("cmd.exe") not to start new process every time.

Is there some solutions for it ?

I hope somebody understand what I'm talking about ;)

Upvotes: 5

Views: 5053

Answers (2)

Ben
Ben

Reputation: 6059

It sounds like you ought to investigate redirecting the standard input - be sure to also set psi.UseShellExecute to false. You'll probably also want to redirect standard output, so you can have some way of knowing what your child process is doing.

Read more about redirection here.

Upvotes: 0

user195488
user195488

Reputation:

I recommend you utilize a batch file to script the execution of your executables and call your batch file instead. Or, you can do this -

    Process p = new Process();
    ProcessStartInfo info = new ProcessStartInfo();
    info.FileName = "cmd.exe";
    info.RedirectStandardInput = true;
    info.UseShellExecute = false;

    p.StartInfo = info;
    p.Start();

    using (StreamWriter sw = new StreamWriter(p.StandardInput))
    {
        if (sw.BaseStream.CanWrite)
        {
            sw.WriteLine("mysql -u root -p");
            sw.WriteLine("mypassword");
            sw.WriteLine("use mydb;");
        }
    }

Upvotes: 10

Related Questions