public string CmdPc(string cmdinput)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.CreateNoWindow = true;
            try
            {
                p.Start();
                p.StandardInput.WriteLine(cmdinput);
                p.StandardInput.WriteLine("exit");
                string ss=p.StandardOutput.ReadToEnd();
                p.WaitForExit();
                p.Close();
                return ss;
            }
            catch 
            { 
                string ss = "命令执行失败";
                return ss;
            }
        }

解决方案 »

  1.   

    LZ可直接放在IDE中,将光标放在属性上或去掉属性重新点一次,看提示或解释即可明白
      

  2.   

    運行cmd命令行執行cmdinput的命令。
      

  3.   

                Process p = new Process();    //创建进程实例
                p.StartInfo.FileName = "cmd.exe";    //cmd 命令 就是控制台
                p.StartInfo.UseShellExecute = false;   //是否使用操作系统外壳程序启动进程
                p.StartInfo.RedirectStandardInput = true;   // 标准输入
                p.StartInfo.RedirectStandardOutput = true;   // 标准输出
                p.StartInfo.CreateNoWindow = true;  //无显示界面
                try 
                { 
                    p.Start();  //启动进程
                    p.StandardInput.WriteLine(cmdinput);   // 执行命令脚本
                    p.StandardInput.WriteLine("exit");     // 关闭控制台
                    string ss=p.StandardOutput.ReadToEnd();   //将输出内容赋给变量ss
                    p.WaitForExit();   //如果这个外部程序没有结束运行则对其强行终止
                    p.Close();  // 关闭进程
                    return ss;   // 方法返回值
                } 
                catch  
                {  
                    string ss = "命令执行失败"; 
                    return ss;   // 异常返回值
                }