zoukankan      html  css  js  c++  java
  • 执行CMD命令

    #region 执行cmd命令
    /// <summary>
    /// 执行cmd命令
    /// </summary>
    /// <param name="commandText"></param>
    /// <returns></returns>
    private string ExeCommand(string commandText)
    {
    Process p = new Process(); //创建并实例化一个操作进程的类:Process
    p.StartInfo.FileName = "cmd.exe"; //设置要启动的应用程序
    p.StartInfo.UseShellExecute = false; //设置是否使用操作系统shell启动进程
    p.StartInfo.RedirectStandardInput = true; //指示应用程序是否从StandardInput流中读取
    p.StartInfo.RedirectStandardOutput = true; //将应用程序的输入写入到StandardOutput流中
    p.StartInfo.RedirectStandardError = true; //将应用程序的错误输出写入到StandarError流中
    p.StartInfo.CreateNoWindow = true; //是否在新窗口中启动进程
    string strOutput = null;
    try
    {
    p.Start();
    p.StandardInput.WriteLine(commandText); //将CMD命令写入StandardInput流中
    p.StandardInput.WriteLine("exit"); //将 exit 命令写入StandardInput流中
    strOutput = p.StandardOutput.ReadToEnd(); //读取所有输出的流的所有字符
    p.WaitForExit(); //无限期等待,直至进程退出
    p.Close(); //释放进程,关闭进程
    }
    catch (Exception e)
    {
    strOutput = e.Message;
    }
    return strOutput;

    }
    #endregion

    使用方法:

     这是一个可执行文件的路径(包括可执行文件名称),也可以是cmd的命令行语句

  • 相关阅读:
    Java static修饰符小记
    nginx的使用
    Java 日期时间格式化
    Java Annotation使用详解
    栈的应用-四则运算表达式
    计算机网络——学习笔记
    Python __builtin__模块
    搭建Harbor私有库
    Prometheus k8s方式安装
    Day4_字典循环
  • 原文地址:https://www.cnblogs.com/zhudezhiwansui/p/7264105.html
Copyright © 2011-2022 走看看