用户以编程方式在cmd中输入命令

时间:2018-02-22 16:29:27

标签: c# winforms cmd

我想构建一个可以提示CMD的Windows应用程序。但是我想让用户以编程方式输入命令。在下面的代码中,我启用了调用CMD,但命令(例如ipconfig)只是硬编码。我希望用户输入其他命令。

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    Process process = new Process();
    process.StartInfo.FileName = "cmd.exe";
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.UseShellExecute = false;
    process.Start();
    process.StandardInput.WriteLine("ipconfig");
    process.StandardInput.Flush();
    process.StandardInput.Close();
    process.WaitForExit();

    string s = process.StandardOutput.ReadToEnd();

    richTextBox1.Text = s;
}

1 个答案:

答案 0 :(得分:1)

你可以这样做....

 // you can populate command from user input
 String command  = "ipconfig";
  // or
 command = userTxt.Text;
 process.StandardInput.WriteLine(command );