本文實現(xiàn)以管理員權(quán)限打開window終端cmd,并在終端里執(zhí)行多條指令的功能。
以掛載vhd虛擬盤、卸載vhd虛擬盤為例。
一、掛載vhd虛擬盤
C#工程 vhdAttach, 生成vhdAttach.exe,vhdAttach.exe的功能為:啟動windows終端cmd.exe,讀取attach-vhd.txt中的內(nèi)容,并在終端里執(zhí)行attach-vhd.txt中的多條指令。
注意:要設(shè)置vhdAttach.exe的權(quán)限(右鍵點擊vhdAttach.exe,屬性,兼容性,勾選"以管理員身份運行此程序")。
attach-vhd.txt的內(nèi)容如下:
/K runas /savecred /user:administrator cmd
diskpart
select vdisk file="E:\1.vhd"
attach vdisk
exit
工程使用的參數(shù)在App.config里可配置,配置參數(shù)如下:
? ? ?<appSettings>
?? ??? ?<add key="filePath" value="D:/attach-vhd.txt"/>
?? ??? ?<add key="processName" value="vhdAttach"/>
?? ??? ?<add key="waitTime" value="3000"/>
? ? </appSettings>
以管理員權(quán)限運行vhdAttach.exe的命令,第一次運行時提示輸入管理員密碼,之后不必再輸入:runas /user:administrator /savecred vhdAttach
執(zhí)行效果,掛載vhd虛擬盤(注意vhd虛擬盤的路徑,vhd虛擬盤的初始化見相關(guān)教程)。
vhdAttach工程的主要代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace vhdAttach
{
? ? class Program
? ? {
? ? ? ? //private static string CmdPath = @"C:\Windows\System32\cmd.exe";
? ? ? ? private static string CmdPath = "cmd.exe";
? ? ? ? /// <summary>
? ? ? ? /// 執(zhí)行cmd命令 返回cmd窗口顯示的信息
? ? ? ? /// 多命令請使用批處理命令連接符:
? ? ? ? /// <![CDATA[
? ? ? ? /// &:同時執(zhí)行兩個命令
? ? ? ? /// |:將上一個命令的輸出,作為下一個命令的輸入
? ? ? ? /// &&:當&&前的命令成功時,才執(zhí)行&&后的命令
? ? ? ? /// ||:當||前的命令失敗時,才執(zhí)行||后的命令]]>
? ? ? ? /// </summary>
? ? ? ? ///<param name="cmd">執(zhí)行的命令</param>
? ? ? ? public static string RunCmd(string[] cmdList) ?//List<string> cmdList
? ? ? ? { ? ? ? ? ? ?
? ? ? ? ? ? using (Process p = new Process())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? p.StartInfo.FileName = CmdPath;
? ? ? ? ? ? ? ? p.StartInfo.UseShellExecute = false; //是否使用操作系統(tǒng)shell啟動
? ? ? ? ? ? ? ? p.StartInfo.RedirectStandardInput = true; //接受來自調(diào)用程序的輸入信息
? ? ? ? ? ? ? ? p.StartInfo.RedirectStandardOutput = true; //由調(diào)用程序獲取輸出信息
? ? ? ? ? ? ? ? p.StartInfo.RedirectStandardError = true; //重定向標準錯誤輸出
? ? ? ? ? ? ? ? p.StartInfo.CreateNoWindow = true; //不顯示程序窗口
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? var pp = p.Start();//啟動程序
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? foreach(string cmdline in cmdList)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? p.StandardInput.WriteLine(cmdline);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? p.StandardInput.AutoFlush = true;
? ? ? ? ? ? ? ? //獲取cmd窗口的輸出信息
? ? ? ? ? ? ? ? string output = p.StandardOutput.ReadToEnd();
? ? ? ? ? ? ? ? p.WaitForExit();//等待程序執(zhí)行完退出進程
? ? ? ? ? ? ? ? p.Close();
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? return output;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? ? ? ? ?
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? Action exit_delegate = killProcess; ? ? ? ? ? ?
? ? ? ? ? ? exit_delegate.BeginInvoke(null,null);
? ? ? ? ? ? try
? ? ? ? ? ? { ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? string filePath = System.Configuration.ConfigurationSettings.AppSettings.Get("filePath");
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? string[] paramList = File.ReadAllLines(filePath);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? string put = RunCmd(paramList); ? ?//執(zhí)行命令 ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? Console.WriteLine(put); ? ? ? ?//控制臺輸出返回結(jié)果
? ? ? ? ? ? }
? ? ? ? ? ? catch(Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? static void killProcess()
? ? ? ? { ? ? ? ? ? ?
? ? ? ? ? ? string waitTimeStr = System.Configuration.ConfigurationSettings.AppSettings.Get("waitTime");
? ? ? ? ? ? int waitTime = int.Parse(waitTimeStr);
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Thread.Sleep(waitTime);
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? ? ? string processName = System.Configuration.ConfigurationSettings.AppSettings.Get("processName");
? ? ? ? ? ? Process[] pros = Process.GetProcessesByName(processName);
? ? ? ? ? ? foreach (Process p in pros)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? p.Kill();
? ? ? ? ? ? }
? ? ? ? }文章來源:http://www.zghlxwxcb.cn/news/detail-494725.html
? ? }
}文章來源地址http://www.zghlxwxcb.cn/news/detail-494725.html
二、卸載vhd虛擬盤
C#工程 vhdDetach, 生成vhdDetach.exe,vhdDetach.exe的功能為:啟動windows終端cmd.exe,讀取detach-vhd.txt中的內(nèi)容,并在終端里執(zhí)行detach-vhd.txt中的多條指令。
注意:要設(shè)置vhdDetach.exe的權(quán)限(右鍵點擊vhdDetach.exe,屬性,兼容性,勾選"以管理員身份運行此程序")
detach-vhd.txt的內(nèi)容如下:
/K runas /savecred /user:administrator cmd
diskpart
select vdisk file="E:\1.vhd"
detach vdisk
exit
工程使用的參數(shù)在App.config里可配置,配置參數(shù)如下:
? ? ?<appSettings>
?? ??? ?<add key="filePath" value="D:/detach-vhd.txt"/>
?? ??? ?<add key="processName" value="vhdDetach"/>
?? ??? ?<add key="waitTime" value="3000"/>
? ? </appSettings>
以管理員權(quán)限運行vhdDetach.exe的命令,第一次運行時提示輸入管理員密碼,之后不必再輸入:runas /user:administrator /savecred vhdDetach
執(zhí)行效果,卸載vhd虛擬盤(注意vhd虛擬盤的路徑,vhd虛擬盤的初始化見相關(guān)教程)。
vhdDetach工程的主要代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace vhdDetach
{
? ? class Program
? ? {
? ? ? ? //private static string CmdPath = @"C:\Windows\System32\cmd.exe";
? ? ? ? private static string CmdPath = "cmd.exe";
? ? ? ? /// <summary>
? ? ? ? /// 執(zhí)行cmd命令 返回cmd窗口顯示的信息
? ? ? ? /// 多命令請使用批處理命令連接符:
? ? ? ? /// <![CDATA[
? ? ? ? /// &:同時執(zhí)行兩個命令
? ? ? ? /// |:將上一個命令的輸出,作為下一個命令的輸入
? ? ? ? /// &&:當&&前的命令成功時,才執(zhí)行&&后的命令
? ? ? ? /// ||:當||前的命令失敗時,才執(zhí)行||后的命令]]>
? ? ? ? /// </summary>
? ? ? ? ///<param name="cmd">執(zhí)行的命令</param>
? ? ? ? public static string RunCmd(string[] cmdList) ?//List<string> cmdList
? ? ? ? {
? ? ? ? ? ? //cmd = cmd.Trim().TrimEnd('&') + "&exit";//說明:不管命令是否成功均執(zhí)行exit命令,否則當調(diào)用ReadToEnd()方法時,會處于假死狀態(tài)
? ? ? ? ? ? using (Process p = new Process())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? p.StartInfo.FileName = CmdPath;
? ? ? ? ? ? ? ? p.StartInfo.UseShellExecute = false; //是否使用操作系統(tǒng)shell啟動
? ? ? ? ? ? ? ? p.StartInfo.RedirectStandardInput = true; //接受來自調(diào)用程序的輸入信息
? ? ? ? ? ? ? ? p.StartInfo.RedirectStandardOutput = true; //由調(diào)用程序獲取輸出信息
? ? ? ? ? ? ? ? p.StartInfo.RedirectStandardError = true; //重定向標準錯誤輸出
? ? ? ? ? ? ? ? p.StartInfo.CreateNoWindow = true; //不顯示程序窗口
? ? ? ? ? ? ? ? var pp = p.Start();//啟動程序
? ? ? ? ? ? ? ? //向cmd窗口寫入命令
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? foreach (string cmdline in cmdList)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? p.StandardInput.WriteLine(cmdline);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? p.StandardInput.AutoFlush = true;
? ? ? ? ? ? ? ? //獲取cmd窗口的輸出信息
? ? ? ? ? ? ? ? string output = p.StandardOutput.ReadToEnd();
? ? ? ? ? ? ? ? p.WaitForExit();//等待程序執(zhí)行完退出進程
? ? ? ? ? ? ? ? p.Close();
? ? ? ? ? ? ? ? return output;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? Action exit_delegate = killProcess;
? ? ? ? ? ? exit_delegate.BeginInvoke(null, null);
? ? ? ? ? ? try
? ? ? ? ? ? { ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? string filePath = System.Configuration.ConfigurationSettings.AppSettings.Get("filePath");
? ? ? ? ? ? ? ? string[] paramList = File.ReadAllLines(filePath);
? ? ? ? ? ? ? ? string put = RunCmd(paramList); ? ?//執(zhí)行命令 ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? Console.WriteLine(put); ? ? ? ?//控制臺輸出返回結(jié)果
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? static void killProcess()
? ? ? ? { ? ? ? ? ? ?
? ? ? ? ? ? string waitTimeStr = System.Configuration.ConfigurationSettings.AppSettings.Get("waitTime");
? ? ? ? ? ? int waitTime = int.Parse(waitTimeStr);
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Thread.Sleep(waitTime); ?//
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? ? ? string processName = System.Configuration.ConfigurationSettings.AppSettings.Get("processName");
? ? ? ? ? ? Process[] pros = Process.GetProcessesByName(processName);
? ? ? ? ? ? foreach (Process p in pros)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? p.Kill();
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
到了這里,關(guān)于實現(xiàn)以管理員權(quán)限打開window終端cmd,并在終端里執(zhí)行多條指令的功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!