1. 程式人生 > 實用技巧 >Cmd重定向(轉載)

Cmd重定向(轉載)

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;
}
下載地址:https://www.cnblogs.com/publiter/p/13298468.html