码迷,mamicode.com
首页 > 其他好文 > 详细

Cmd重定向

时间:2020-07-14 13:21:31      阅读:54      评论:0      收藏:0      [点我收藏+]

标签:tput   output   bool   win   depend   操作系统   stat   white   获取   

1、执行单条cmd命令

public static string ExecuteCmd(string command)
{
    Process p = new Process();
    p.StartInfo.FileName = "cmd.exe";
    p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
    p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
    p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
    p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
    p.StartInfo.CreateNoWindow = true;//不显示程序窗口
    //p.StartInfo.Arguments = "/c " + command;///c代表执行命令后关闭cmd.exe /k参数则不关闭
    p.Start();//启动程序
    //消除三行启动信息
    p.StandardOutput.ReadLine();
    p.StandardOutput.ReadLine();
    p.StandardOutput.ReadLine();
    //向cmd窗口发送输入信息
    p.StandardInput.WriteLine(command);
    p.StandardInput.WriteLine("exit");
    string result = p.StandardOutput.ReadToEnd().Replace(Environment.CurrentDirectory, "");
    string error = p.StandardError.ReadToEnd();
    if (!string.IsNullOrWhiteSpace(error))
        result = result + "\r\n<Error Message>:\r\n" + error;
    p.WaitForExit();
    p.Close();
    return result;
}

2、一次执行多条cmd命令

public static string ExecuteCmd(string[] commands)
{
    Process p = new Process();
    p.StartInfo.FileName = "cmd.exe";
    p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
    p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
    p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
    p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
    p.StartInfo.CreateNoWindow = true;//不显示程序窗口
    //p.StandardInput.AutoFlush = true;//每次写入命令后刷新,报未StandardInput未重定向
    //p.StartInfo.Arguments = "/c " + command;///c代表执行命令后关闭cmd.exe /k参数则不关闭
    p.Start();//启动程序
    //消除三行启动信息
    p.StandardOutput.ReadLine();
    p.StandardOutput.ReadLine();
    p.StandardOutput.ReadLine();

    //向cmd窗口发送输入信息
    foreach (string cmd in commands)
    {
        p.StandardInput.WriteLine(cmd);
        p.StandardInput.Flush();
    }
    p.StandardInput.WriteLine("exit");
    string result = p.StandardOutput.ReadToEnd().Replace(Environment.CurrentDirectory, "");
    string error = p.StandardError.ReadToEnd();
    if (!string.IsNullOrWhiteSpace(error))
        result = result + "\r\n<Error Message>:\r\n" + error;
    p.WaitForExit();
    p.Close();

    return result;
}

3、利用cmd重定向注册DLL、创建服务、加载证书

public static string RegsvrFile(string filePath, bool reg = true, bool fileBit64 = false)
{
    string result;
    string[] commands = { "", "" };
    if (!fileBit64 && Environment.Is64BitOperatingSystem)
        commands[0] = "cd /d %WinDir%\\SysWOW64";
    if (reg)
        commands[1] = "regsvr32 /s \"" + filePath + "\"";
    else
        commands[1] = "regsvr32 /u /s \"" + filePath + "\"";
    if (string.IsNullOrWhiteSpace(commands[0]))
        result = CmdOper.ExecuteCmd(commands[1]);
    else
        result = CmdOper.ExecuteCmd(commands);
    return result;
}

public static string ScCreate(string filePath, string serviceName, string displayName, string description, string start="auto", string depend=null)
{
    string result;
    string[] commands = { "", "" };
    if (string.IsNullOrWhiteSpace(depend))
    {
        commands[0] = string.Format("sc create {0} binPath= \"{1}\" type= share start= {2} error= ignore DisplayName= \"{3}\"",
            serviceName, filePath, start, displayName);
    }
    else
    {
        commands[0] = string.Format("sc create {0} binPath= \"{1}\" type= share start= {2} error= ignore DisplayName= \"{3}\" depend= {4}",
            serviceName, filePath, start, displayName, depend);
    }
    if (!string.IsNullOrWhiteSpace(description))
    {
        commands[1] = string.Format("sc description {0} \"{1}\"", serviceName, description);
    }
    if (string.IsNullOrWhiteSpace(commands[1]))
        result = CmdOper.ExecuteCmd(commands[0]);
    else
        result = CmdOper.ExecuteCmd(commands);

    return result;
}

public static string ScStart(string serviceName)
{
    string command = "net start " + serviceName;
    return CmdOper.ExecuteCmd(command);
}

public static string ScStop(string serviceName)
{
    string command = "net stop " + serviceName;
    return CmdOper.ExecuteCmd(command);
}

public static string ScDelete(string serviceName)
{
    string[] commands = { "net stop " + serviceName, "sc delete " + serviceName };
    return CmdOper.ExecuteCmd(commands);
}

public static string CertFile(string filePath, bool install=true)
{
    string result;
    string[] commands = { "", "" };
    if (Environment.Is64BitOperatingSystem)
        commands[0] = "cd /d %WinDir%\\SysWOW64";
    if (install)
        commands[1] = "certutil -addstore root \"" + filePath + "\"";
    else
        commands[1] = "certutil -delstore root \"" + filePath + "\"";
    if (string.IsNullOrWhiteSpace(commands[0]))
        result = CmdOper.ExecuteCmd(commands[1]);
    else
        result = CmdOper.ExecuteCmd(commands);
    return result;
}

Cmd重定向

标签:tput   output   bool   win   depend   操作系统   stat   white   获取   

原文地址:https://www.cnblogs.com/publiter/p/13298468.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!