0

I write test console program. This program execute cmd with two line command. But how it's do? Instead of this large code, how write more easy code?

String command = @"cd c:\\test";//command get to current folder 
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = command;
startInfo.RedirectStandardOutput = true;
using (Process exeProcess = Process.Start(startInfo))
{
    exeProcess.WaitForExit();
}
String command = @"echo 'Hello world' > test.txt";//command write Hello world to text file
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = command;
startInfo.RedirectStandardOutput = true;
using (Process exeProcess = Process.Start(startInfo))
{
    exeProcess.WaitForExit();
}
1
  • no two commands necessaray here: > c:\test\test.txt" echo Hello World Commented Mar 31, 2024 at 8:12

2 Answers 2

2

Use the & operator.

For example:

dir & echo foo

For yours:

cd c:\\test & echo 'Hello world' > test.txt

Also see: How to run two commands in one line in Windows CMD?

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

Comments

0

you can put you command in a batch file yourcmd.bat, in your case the yourcmd.bat will like this:

cd c:\test
echo "Hello world" > test.txt

or

cd c:\test & echo "Hello world" > test.txt

then you can just call System.Diagnostics.Process.Start("yourcmd.bat"); method, this works.

Comments

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.