• 执行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("写入成功");
                    }
                }
            }
    
  • 相关阅读:
    cad 画图面板的尺寸大小定义
    CAD中如何将图形按一定的比例放大或缩小
    数组模拟栈和队列
    41. 缺失的第一个正数
    98. 验证二叉搜索树
    1220. 统计元音字母序列的数目
    网络编程--select模型(总结)
    网络编程--C/S模型(总结)
    C++内存管理(堆栈内存的区别、==和equal的区别)
    condition_variable-介绍和使用
  • 原文地址:https://www.cnblogs.com/jysun/p/3934803.html
Copyright © 2020-2023  润新知