国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

實現(xiàn)以管理員權(quán)限打開window終端cmd,并在終端里執(zhí)行多條指令的功能

這篇具有很好參考價值的文章主要介紹了實現(xiàn)以管理員權(quán)限打開window終端cmd,并在終端里執(zhí)行多條指令的功能。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。


本文實現(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


二、卸載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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Win11/Windows11設(shè)置始終以管理員身份運行cmd窗口

    Win11/Windows11設(shè)置始終以管理員身份運行cmd窗口

    在使用Windows進行開發(fā)時,我們經(jīng)常需要使用管理員身份運行cmd窗口, 但是每次打開都需要右鍵\\\"以管理員身份運行\(zhòng)\\",比較浪費時間, 下面將介紹在Win11/Windows11系統(tǒng)中,設(shè)置始終以管理員身份運行cmd窗口! ? ? ? ? 在搜索欄輸入終端,點擊打開。 ????????如下圖所示: ?

    2024年02月12日
    瀏覽(24)
  • windows腳本獲取管理員權(quán)限修改host

    很多時候我們常常需要通過管理員權(quán)限執(zhí)行腳本,腳本可能涉及到一些受保護信息的訪問,我們寫個簡單的腳本來更改host文件,host文件就是需要管理員權(quán)限才能訪問的啟動腳本時先檢查是否有管理員權(quán)限,如果沒有就調(diào)用授權(quán)腳本進行管理員授權(quán)打開,給用戶彈出需要管理

    2024年02月14日
    瀏覽(24)
  • 要在Internet Explorer 模式下打開此頁面,請使用管理員權(quán)限重新安裝 Microsoft Edge.

    要在Internet Explorer 模式下打開此頁面,請使用管理員權(quán)限重新安裝 Microsoft Edge.

    windows 11 更新后 Edge 不能用 Internet Explorer 模式了,導(dǎo)致一些老網(wǎng)站插件無法使用。 創(chuàng)建 .reg 文件,內(nèi)容如下 雙擊運行reg文件

    2024年02月11日
    瀏覽(332)
  • Windows開發(fā):服務(wù)程序啟動有管理員權(quán)限的界面程序

    Windows開發(fā):服務(wù)程序啟動有管理員權(quán)限的界面程序

    本章介紹Windows桌面開發(fā)中,服務(wù)程序如何啟動有管理員權(quán)限的界面進程。 在這種情況下,以下幾點需要弄清楚: Windows的服務(wù)是什么 Microsoft Windows 服務(wù)(過去稱為 NT 服務(wù))允許用戶創(chuàng)建可在其自身的 Windows 會話中長時間運行的可執(zhí)行應(yīng)用程序。 這些服務(wù)可在計算機啟動時自

    2024年02月11日
    瀏覽(16)
  • 要在Internet Explorer模式下打開此頁面,請使用管理員權(quán)限重新安裝Microsoft Edge 問題解決

    首先就是使用管理員模式安裝最新版的edge; 如果然提示這個,新建一個reg文件,內(nèi)容如下: 直接執(zhí)行就行, 如果沒有解決問題,請檢查ie瀏覽器是否可以正常打開,不能的話就需要重裝ie了 。 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

    2024年02月11日
    瀏覽(102)
  • Windows 開機啟動腳本 (不詢問自動以管理員權(quán)限運行bat)

    獲取開機啟動文件夾 之后將“你的.bat”文件放入“開機啟動文件夾”內(nèi)即可 快捷鍵Win+R,zhixing shell:startup 打開用戶級的開機自啟文件夾 或者cmd執(zhí)行: 之后將“你的.bat”文件放入“開機啟動文件夾”內(nèi)即可 相關(guān)參考: Windows設(shè)置程序開機自啟動的幾種方法(整理發(fā)布) 使用

    2024年02月13日
    瀏覽(21)
  • windows10使用administrator登錄后出現(xiàn)“無法使用內(nèi)置管理員賬戶打開應(yīng)用”的解決方法

    windows10使用administrator登錄后出現(xiàn)“無法使用內(nèi)置管理員賬戶打開應(yīng)用”的解決方法

    解決方法: 修改組注冊表。 ? 詳細步驟: ??????1. 【W(wǎng)in】+【R】打開“運行頁面”,輸入:【regedit】進入注冊表編輯器。 2. 依次打開: 【HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionPolicies System】,在右側(cè)找到【FilterAdministratorToken】,雙擊后將數(shù)值數(shù)據(jù)改為【1】

    2024年02月12日
    瀏覽(88)
  • C#實現(xiàn)軟件開機自啟動(不需要管理員權(quán)限)

    目錄 原理簡介 使用方法 完整代碼 本文參考C#/WPF/WinForm/程序?qū)崿F(xiàn)軟件開機自動啟動的兩種常用方法,將里面中的第一種方法做了封裝成 AutoStart 類,使用時直接兩三行代碼就可以搞定。 自啟動的原理是 將軟件的快捷方式創(chuàng)建到計算機的自動啟動目錄下(不需要管理員權(quán)限)

    2024年02月11日
    瀏覽(35)
  • Python 實現(xiàn)程序自動以管理員權(quán)限運行的方法

    由于Windows的安全機制,Python寫的腳本缺少了管理員權(quán)限,運行就會受到一些限制。 文章介紹Python 腳本自動以管理員權(quán)限運行的方法, 也就是如果腳本不是以管理員運行,就自動提升到管理員權(quán)限。 將python提升到管理員權(quán)限運行需要調(diào)用 ShellExecute 這個API函數(shù)。 ShellExecute 函

    2024年02月12日
    瀏覽(30)
  • WinForm實現(xiàn)管理員權(quán)限運行的三種方式

    WinForm實現(xiàn)管理員權(quán)限運行的三種方式

    來源:https://mp.weixin.qq.com/s/ydBWABy7kwOWxNCQu4qYMA ? 在visual studio開發(fā)winform程序,生成msi安裝包以后,代碼運行似乎沒有問題。但是,若是軟件安裝到了C盤,軟件在執(zhí)行某些操作,比如寫文件、讀文件等操作時,有可能會因為操作系統(tǒng)用戶權(quán)限不足導(dǎo)致讀寫不成功。關(guān)鍵這時候軟

    2024年02月04日
    瀏覽(18)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包