c# open cmd.exe process and Execute multiple commands

10,131

There three things you can do to achieve what you want. The easiest is to set the working directory of the process through ProcessStartInfo. This way you will only have to execute the command to start the VPN client.

The second option is redirecting the input and output of the process. (Also done through the ProcessStartInfo) This is something you want to do if you need to send more input to the process, or when you want to retrieve the output of the process you just started.

The third option is to combine the two commands with the & symbol. Using the & symbol makes cmd.exe execute the two commands sequentially (See here for an overview of the available symbols). Using this option will result in a command like this: /c cd path & vpnclient.

However because you just want to change the working directory of the process using the first option makes your code more readable. Because people reading your code do not need to know the & symbol in bash to understand what your code does. Changing the working directoy is done with the WorkingDirectory (MSDN) property of ProcessStartInfo (MSDN). See the following code:

var processInfo = new ProcessStartInfo("cmd.exe", @"/c vpnclient connect user validuser pwd validpassword nocertpwd validconnectionentry ");
processInfo.UseShellExecute = false;
processInfo.WorkingDirectory = path;
Share:
10,131
Dev
Author by

Dev

I am a student who is enthusiastic with Software Development. Coding is the best way for me to spend my time. I program in C#, ASP.NET MVC, jQuery, Javascript, CSS3, HTML5 and Java. Coding is my passion and I love indulging in it everyday.

Updated on June 28, 2022

Comments

  • Dev
    Dev almost 2 years

    I would like to be able to open cmd and execute two commands from the window. First I would like to navigate to a particular directory where I can then run the second command from. Running a single command is pretty easy as this is all I have to do:

    string path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + @"\Cisco Systems\VPN Client\";
    
            Process process = new Process();
    
            ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe", @"/c cd " + path );
    
            process.StartInfo = processInfo;
    
            process.Start();
    

    However am not sure of the way to add the second argument so it runs after cmd runs the first command. Some research led me to this code snippet. Am unsure if this works since my aim is to start cisco vpn client from cmd and this seems not to start it. Here is the code:

    string path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + @"\Cisco Systems\VPN Client\";
    
            Process process = new Process();
    
            ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe", @"/c cd " + path + "-t vpnclient connect user validuser pwd validpassword nocertpwd validconnectionentry ");
    
            process.StartInfo = processInfo;
    
            process.Start();
    

    I once started the vpn client from cmd with the credentials just to make sure they were valid and it worked but I cant pull it off via C# programmatically.

    Regards.

  • Dev
    Dev about 9 years
    I used the first option you specified and set the working directory first then execute the command. This works for my case but once I try all the other options you specified, I'll leave a comment. Thanks for pointing out the several ways.
  • TheDutchDevil
    TheDutchDevil about 9 years
    Since the second option is overkill I did not include any more information on it. See the following tutorial for some pointers on how to achieve that.