首页 > 代码库 > 执行CMD代码

执行CMD代码

/// <summary>        /// 发送CMD命令(执行命令行)        /// </summary>        public static void SendCMD(string pwd)        {            string route = ConfigurationManager.AppSettings["Rout"];            try            {                var _p = new Process();                //Process类有一个StartInfo属性,这是ProcessStartInfo类,包括了一些属性和方法                _p.StartInfo.FileName = "cmd.exe";                //执行的命令及参数                //_p.StartInfo.Arguments = "/c " + " net user zwsc " + pwd;                _p.StartInfo.Arguments = "/c " + " net user " + ConfigurationManager.AppSettings["userName"] + " " + pwd;                _p.StartInfo.UseShellExecute = false; //关闭Shell的使用                _p.StartInfo.RedirectStandardInput = true;                _p.StartInfo.RedirectStandardOutput = true;                _p.StartInfo.RedirectStandardError = true;                _p.StartInfo.CreateNoWindow = true;//设置不显示窗口                _p.Start(); //启动进程                string _standardOutput = _p.StandardOutput.ReadToEnd();                string _errorOutput = _p.StandardError.ReadToEnd();                string[] _message = { _standardOutput, _errorOutput };                _p.WaitForExit();            }            catch (Exception ex)            {                string file = route + DateTime.Now.ToString("yyyyMMddhhmmss") + ".txt";                string content = ex.Message;                if (File.Exists(file))                {                    //MessageBox.Show("存在此文件!");                    FileStream myFs = new FileStream(file, FileMode.Open);                    StreamWriter mySw = new StreamWriter(myFs);                    mySw.Write(content);                    mySw.Close();                    myFs.Close();                }                else                {                    FileStream myFs = new FileStream(file, FileMode.Create);                    StreamWriter mySw = new StreamWriter(myFs);                    mySw.Write(content);                    mySw.Close();                    myFs.Close();                    //MessageBox.Show("写入成功");                }            }        }

 

执行CMD代码