简体   繁体   中英

c# open cmd.exe process and Execute multiple commands

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.

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;

You can use & to execute next command or && to execute following command only if the previous one succeeded.

Examples:

dir /b & cls

and

taskkill /f /im explorer.exe && start explorer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM