user3338893
user3338893

Reputation: 977

Using C#, how do I run commands from a new cmd.exe that is created by a batch script?

I have a batch script that sets up some environment variables and then sets up a new cmd.exe within the currently executing cmd.exe.

Example of what the batch script looks like:

setlocal
set foo=bar
cmd.exe

How do I start a new Process() object that calls this batch script, then is able to pipe the StandardInput into the new instance of the cmd.exe?

This is my current code that doesn't work:

ProcessStartInfo procStartInfo = new ProcessStartInfo();
procStartInfo.FileName = @"C:\path\to\test.bat";

using (Process proc = Process.Start(procStartInfo))
{
    proc.StandardInput.WriteLine("echo %foo%"); // InvalidOperationException
}

Upvotes: 3

Views: 104

Answers (1)

LB2
LB2

Reputation: 4860

You have to use ProcessStartInfo class and set RedirectStandardInput flag, and then provide a stream that you will supply data with.

Upvotes: 1

Related Questions