在c#中单击按钮时将参数传递给命令行

时间:2013-09-11 12:56:09

标签: c# .net

我正在尝试传递一串将打开命令行的参数,并在单击按钮时传递指定的参数。我试图将一个值定义为文本框的内容(对框中列出的IP地址进行常量ping)。它将打开命令行,但不会传递任何参数,任何人都可以协助吗?

private void Pingbutt_Click(object sender, EventArgs e)
    {
        System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
        proc.FileName = @"C:\windows\system32\cmd.exe";
        String s = Cmiptxt.Text;
        proc.Arguments = (@"c/ ping" + s + "-t");
        System.Diagnostics.Process.Start(proc);
    }

2 个答案:

答案 0 :(得分:2)

看起来你在ip之前和之后错过了一个空格,string.Format会让你更容易阅读

proc.Arguments = string.Format("c/ ping {0} -t", s);

或者,实现此目的的一种简单方法是使用Process.Start(string, string)重载

Process.Start("cmd.exe", string.Format("c/ ping {0} -t", s));

答案 1 :(得分:0)

我尝试这个。不能完全正常工作。 cmd打开。不运行命令。

 private void btnDownload_Click(object sender, EventArgs e)
    {

       Process.Start("cmd.exe", string.Format("ipconfig"));
    }
相关问题