1

I am currently trying to "convert" a bash script I wrote to C#. This script starts a program in a shell and then executes a few commands in this shell and looks similar to this:

cd /$MYPATH
./executible -s << EOF
start_source_probe -hardware_name "USB" -device_name "@1: EP3C(10|5)"
write_source_data -instance_index 0 -value "11111"
write_source_data -instance_index 0 -value "10111"
write_source_data -instance_index 0 -value "00111"
exit
EOF

Now I would like to do the same using Visual Studio C#. At the moment my attempt looks like this:

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = "pathToExe\\";
startInfo.FileName= "executible.exe";
startInfo.Arguments= "-s";
//startInfo.UseShellExecute = false;
//startInfo.RedirectStandardInput = true;
Process proc = new Process();
proc.StartInfo = startInfo;
proc.Start();
//StreamWriter myStreamWriter = proc.StandardInput;
//Console.WriteLine("start_source_probe - hardware_name \"USB\" -device_name \"@1: EP3C(10|5)\"");
//Console.WriteLine("write_source_data -instance_index 0 -value \"11111\"");
proc.WaitForExit();

With the comments activated (so with the "//" in code) I manage to open the shell (-s stands for shell) but I wonder how I am able to execute commands in this shell additionally. I managed to execute multiple commands with something like this (as long as I am not starting the shell because destination output differs I guess)

const string strCmdText = "/C command1&command2";
Process.Start("CMD.exe", strCmdText);

I would appreciate it if someone could tell me how to add the argument "-s" and execute commands in the started process.

1
  • I find that multiple commands with cmd.exe is awkward at best. Better off if you write out your commands into a .cmd file and execute that. You'll have "normal" batch syntax in your .cmd file. Commented Feb 7, 2017 at 14:25

1 Answer 1

1

I am not sure if i did understood your question, but you can create a batch file outside your c# code and call It from your c# code like the following :

System.Diagnostics.ProcessStartInfo ProcStartInfo = new System.Diagnostics.ProcessStartInfo("cmd");
ProcStartInfo.RedirectStandardOutput = true;
ProcStartInfo.UseShellExecute = false;
ProcStartInfo.CreateNoWindow = false;
ProcStartInfo.RedirectStandardError = true;
System.Diagnostics.Process MyProcess = new System.Diagnostics.Process();
ProcStartInfo.Arguments = "/c start /wait batch.bat ";
MyProcess.StartInfo = ProcStartInfo;
MyProcess.Start();
MyProcess.WaitForExit();

I added the /wait so your c# code is going to wait for your batch to finish, to pursuit the c# code execution

Sign up to request clarification or add additional context in comments.

3 Comments

Can you tell me how to add a here document like the "<< EOF ......" in my bash script in a batch script or make something with the same result?
I did not understood your question, what do you mean by "add a here document like the" and "in my bash script in a batch script or make"
doesnt matter anymore. I figured out how to do it. "type sometext.txt | myexe.exe -s" helped. Thx for your answer!

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.