2

I am using the following code to open the .exe and then I would like to pass another argument to it:

ProcessStartInfo StartInfo = new ProcessStartInfo();
StartInfo.FileName = "cmd.exe";
StartInfo.Arguments = @"/k set inetroot=c:\depot&set corextbranch=surfacert_v2_blue_kit&c:\depot\tools\path1st\myenv.cmd";
Process.Start(StartInfo);`

Which opens up the window as below. enter image description here

Now I also need to pass "sd sync dirs" which gives me some result and would like to capture the result to a variable.enter image description here

To accomplish this I need to pass two agruments in the ProcessStartInfo.Arguments. How can I add this second argument in the above code to take care of everything in C# code.

Brandon
  • 645
  • 5
  • 13
user1144852
  • 265
  • 3
  • 6
  • 17
  • 1
    `StartInfo.Arguments += " /another-argument"` perhaps? – Steve Jun 17 '14 at 22:58
  • possible duplicate of [How to pass multiples arguments in processStartInfo?](http://stackoverflow.com/questions/15061854/how-to-pass-multiples-arguments-in-processstartinfo) – ClickRick Jun 17 '14 at 23:02

2 Answers2

1

Since its just a string try this:

string[] MyArguments = { "firstarg", "secondarg"};
Process.Start("cmd.exe", String.Join(" ", MyArguments));

Where firstarg and secondarg are your arguments.

EDIT: Oops forgot to tell you ,if your argument contains spaces do this(the example contains 1 argument with spaces-first arg- and 1 without spaces-secondarg):

string[] MyArguments = { "\"first arg\"", "secondarg" };
terrybozzio
  • 4,424
  • 1
  • 19
  • 25
  • Tried but looks like it is not taking the seconde argument. I still see the window as it is shown is the first picture, after running the code. – user1144852 Jun 17 '14 at 23:09
  • edited...forgot to tell you sorry,so if your arguments both have spaces do like in first arg example. – terrybozzio Jun 17 '14 at 23:14
0

Here's an example of passing multiple arguments:

http://msdn.microsoft.com/en-us/library/bfbyhds5.aspx

http://msdn.microsoft.com/en-us/library/53ezey2s.aspx

If you're passing strings you need to take account of the possibility of quotes being included in the subject line or body text. I enlisted some help on this issue with a StackOverflow question.

I ended up with something like this:

// DOS command line
C:\>ConsoleApplication1 "Subject Line Text" "Some body text"

// Web form code-behind
// Pass subject and message strings as params to console app    
ProcessStartInfo info = new ProcessStartInfo();

string arguments = String.Format(@"""{0}"" ""{1}""",
     subjectText.Text.Replace(@"""", @""""""),
     messageText.Text.Replace(@"""", @""""""));
     info.FileName = MAILER_FILEPATH;

Process process = Process.Start(info.FileName, arguments);
Process.Start(info);

// Console application
static void Main(string[] args)
{
    if (args.Length >= 2)
    {
        // Do stuff 
    }
}
Community
  • 1
  • 1
IrishChieftain
  • 15,108
  • 7
  • 50
  • 91