Execute Batch File From C# Console
-
Saturday, August 11, 2012 4:43 PM
Let's say from my C# console application I want to run a batch file that will take two arguments. The two string variables at the top of the C# application will be the string arguments to pass to the batch file. How would I go about doing it?
Here is my code so far my C# console program:
string argumentOne = "Hello World"; string argumentTwo = "How are You"; System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); //startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.FileName = "Greetings.bat";
//How do I insert two arguments?
//startInfo.Arguments = "/C " + message; process.StartInfo = startInfo; process.Start();
My Batch File
CLS @ECHO OFF ECHO %1 %2
- Edited by Oasisfactor Saturday, August 11, 2012 4:43 PM
All Replies
-
Saturday, August 11, 2012 5:15 PM
you can use "arguments" - like this
string argumentOne = "\"Hello World\"";
string argumentTwo = "\"How are You\"";
startInfo.FileName = "Greetings.bat";
startInfo.Arguments = string.Format("{0} {1}",argumentOne,argumentTwo);
// startInfo.Arguments = argumentOne + " " + argumentTwo;// startInfo.Arguments += argumentOne;
// startInfo.Arguments += " ";
// startInfo.Arguments += argumentTwo;process.StartInfo = startInfo; process.Start();
-
Saturday, August 11, 2012 9:45 PM
You need to put double quotes around arguments that contain whitespaces.
Also - because of the /C switch - seems you want to launch
cmd.exe and pass the the batch file also as an argument:
string argumentOne = "Hello World"; string argumentTwo = "How are You"; string batchFile = "Greetings.bat"; System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.FileName = System.Environment.GetEnvironmentVariable("COMSPEC");
startInfo.Arguments = string.Format("/C {0} \"{1}\" \"{2}\"", batchFile, argumentOne, argumentTwo); process.StartInfo = startInfo; process.Start();
Note the quotes will NOT be removed in the batch file.
If you need to remove them, you can do it like this:
@ECHO OFF CLS FOR %%A in (%1) do set ARG1=%%~A FOR %%A in (%2) do set ARG2=%%~A ECHO %ARG1% %ARG2%
Chris
- Edited by Grecian Developer Saturday, August 11, 2012 9:57 PM use cmd.exe as file
- Edited by Grecian Developer Saturday, August 11, 2012 9:58 PM use cmd.exe as FileName
- Proposed As Answer by Lisa ZhuMicrosoft Contingent Staff, Moderator Monday, August 13, 2012 7:01 AM
- Marked As Answer by Lisa ZhuMicrosoft Contingent Staff, Moderator Thursday, August 16, 2012 10:28 AM
-
Sunday, August 12, 2012 7:32 AM
Hi,
Please refer to the below article at http://tad.co.in/?p=401
- Proposed As Answer by Lisa ZhuMicrosoft Contingent Staff, Moderator Monday, August 13, 2012 7:01 AM
- Marked As Answer by Lisa ZhuMicrosoft Contingent Staff, Moderator Thursday, August 16, 2012 10:28 AM

