如何使用C#与多个参数来运行cmd.exe的? [英] How to run cmd.exe using c# with multiple arguments?

查看:698
本文介绍了如何使用C#与多个参数来运行cmd.exe的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code打开.exe文件,然后我想通过换个说法吧:

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.

现在我还需要通过SD同步显示目录,这给了我一些结果,并希望将结果捕捉到一个变量。在这里输入的形象描述

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

要做到这一点我需要通过两个agruments
ProcessStartInfo.Arguments。
我怎样才能在上面code照顾一切在C#code添加此第二个参数。

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.

推荐答案

下面是传递多个参数的例子:

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

如果您正在传递的字符串,你需要考虑到行情的可能性,包括在主题行或正文。我入伍在这个问题上一些帮助,一个计算器<一个href=\"http://stackoverflow.com/questions/8054178/processstartinfo-multiple-arguments\">question.

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.

我结束了这样的事情:

// 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 
    }
}

这篇关于如何使用C#与多个参数来运行cmd.exe的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆