zoukankan      html  css  js  c++  java
  • C#中调用Process执行CMD命令

    C#中调用命令行执行命令行命令,例如在WinForm程序启动时为了防止数据库没有启动,先启动数据库服务。其中需要主要提一点的就是,在有些电脑上启动的CMD程序的默认路径没有在C盘等系统能找到的路径,在这种情况下,批处理就会失败。所以加了强制转换到C盘的操作,以增加程序的容错性和更多的实用性。程序如下:

    /// <summary>
    /// 执行批处理
    /// </summary>
    private void ExecuteBatch()
    {
            Process p = new Process();
                p.StartInfo.FileName = @"C:\WINDOWS\system32\cmd.exe ";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                p.Start();
                p.StandardInput.WriteLine(@"c: ");  //先转到系统盘下
                p.StandardInput.WriteLine(@"cd C:\WINDOWS\system32 ");  //再转到CMD所在目录下
                p.StandardInput.WriteLine(@"net start MSSQLSERVER ");
                p.StandardInput.WriteLine("exit");
                p.WaitForExit();
                p.Close();
                p.Dispose();

    }

  • 相关阅读:
    Go
    Go
    Go -11 Go 框架beego的简单 create run
    文本处理工具之:grep sed awk
    Linux系统布置java项目
    docker 启动mysql 本地连接
    time
    多行查询数据合并成一行进行展示
    settings的使用
    xlsxwriter
  • 原文地址:https://www.cnblogs.com/junior/p/2426355.html
Copyright © 2011-2022 走看看