日期:2014-05-17  浏览次数:20821 次

cmd命令不能用,怎么回事?
先上代码:
        
        private void button1_Click(object sender, EventArgs e)
        {
            string strCmd = this.txbCmd.Text;  //获取命令行
            string strCmdResult = InvokeCmd(strCmd);
            rtbCmdResult.Text = strCmdResult;  //输出到rtbCmdResult控件中
            //MessageBox.Show(strCmdResult);
        }
        private static string InvokeCmd(string cmdArgs)
        {
            string Tstr = "";
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();

            p.StandardInput.WriteLine(cmdArgs);
            p.StandardInput.WriteLine("exit");
            Tstr = p.StandardOutput.ReadToEnd();
            p.Close();
            return Tstr;
        }


比如输入,net user 或 ver 一点击那个button事件,直接又弹出一个新的form1窗口,怎么回事。。请教啦

------解决方案--------------------
就你的代码没有问题。别的地方又new了一个窗口?