zoukankan      html  css  js  c++  java
  • 执行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("写入成功");
                    }
                }
            }
    
  • 相关阅读:
    Google File System(中文翻译)
    Hadoop学习之路一 Single Node Setup
    大数据建模比赛--金融市场板块划分和轮动规律研究.
    华中建模-人脸识别
    计算循环队列的元素个数
    低价租用高性能GPU进行深度学习
    vscode+PyQt+QtDesigner
    mask_rcnn(Keras+TensorFlow)环境搭建_新手向(毕业设计使用,亲测可用)
    博客园美化
    Task1 赛题理解
  • 原文地址:https://www.cnblogs.com/jysun/p/3934803.html
Copyright © 2011-2022 走看看