简体   繁体   English

c#打开cmd.exe进程并执行多个命令

[英]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. 我希望能够打开cmd并从窗口执行两个命令。 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. 但是,不确定添加第二个参数的方式,以便它在cmd运行第一个命令之后运行。 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. 不确定是否可行,因为我的目的是从cmd启动cisco vpn客户端,但这似乎无法启动。 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. 我曾经使用凭据从cmd启动vpn客户端,只是为了确保它们有效并且可以正常工作,但是我无法以编程方式通过C#将其关闭。

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 . 最简单的方法是通过ProcessStartInfo设置流程的工作目录。 This way you will only have to execute the command to start the VPN client. 这样,您只需执行命令即可启动VPN客户端。

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. (也可以通过ProcessStartInfo完成)如果您需要向流程发送更多输入,或者想要检索刚开始的流程的输出,则需要执行此操作。

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). 使用&符号可使cmd.exe依次执行两个命令(有关可用符号的概述,请参见此处 )。 Using this option will result in a command like this: /c cd path & vpnclient . 使用此选项将导致如下命令: /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. 因为阅读您的代码的人无需知道bash中的&符号即可了解您的代码的用途。 Changing the working directoy is done with the WorkingDirectory ( MSDN ) property of ProcessStartInfo ( MSDN ). 更改工作目录是通过ProcessStartInfoMSDN )的WorkingDirectoryMSDN )属性完成的。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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