zoukankan      html  css  js  c++  java
  • c# 程序调用cmd执行命令如SVN.exe

    c# 程序调用cmd执行命令如SVN.exe

    string str = Console.ReadLine();
    
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.WorkingDirectory="C:	ool";//具体第三方exe目录,如snv.exe,没这句命令将无法识别,除非这个工具已加到环境变量中了
                p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
                p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
                p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
                p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
                p.StartInfo.CreateNoWindow = true;//不显示程序窗口
                p.Start();//启动程序
     
                //向cmd窗口发送输入信息
                p.StandardInput.WriteLine(str + "&exit");
    
                p.StandardInput.AutoFlush = true;
                //p.StandardInput.WriteLine("exit");
                //向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死
                //同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令
    
    
    
                //获取cmd窗口的输出信息
                string output = p.StandardOutput.ReadToEnd();
    
                //StreamReader reader = p.StandardOutput;
                //string line=reader.ReadLine();
                //while (!reader.EndOfStream)
                //{
                //    str += line + "  ";
                //    line = reader.ReadLine();
                //}
    
                p.WaitForExit();//等待程序执行完退出进程
                p.Close();
    
    
                Console.WriteLine(output);
    

      

  • 相关阅读:
    【Android】HAL分析
    【qt】QT 的信号与槽机制
    【驱动】DM9000A网卡驱动框架源码分析
    【驱动】LCD驱动(FrameBuffer)分析
    告别我的OI生涯
    诸神的黄昏——北林退役帖合集
    cf592d
    北京林业大学就读体验
    hdu 5442 (ACM-ICPC2015长春网络赛F题)
    JAVA入门[4]-IntelliJ IDEA配置Tomcat
  • 原文地址:https://www.cnblogs.com/8090sns/p/8277013.html
Copyright © 2011-2022 走看看