Answered Execute Batch File From C# Console

  • Saturday, August 11, 2012 4:43 PM
     
      Has Code

    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
     
      Has Code

    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();





    • Edited by Doemae Saturday, August 11, 2012 5:26 PM Modify codes
    • Edited by Doemae Saturday, August 11, 2012 5:28 PM Modify codes
    • Edited by Doemae Saturday, August 11, 2012 5:30 PM code modify -> now with spaces
    • Edited by Doemae Saturday, August 11, 2012 5:30 PM code modify -> now with spaces
    •  
  • Saturday, August 11, 2012 9:45 PM
     
     Answered Has Code
    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


  • Sunday, August 12, 2012 7:32 AM
     
     Answered

    Hi,

        Please refer to the below article at http://tad.co.in/?p=401