讓C#程式以系統管理員身分執行(UAC) [Windows 權限]
讓C#程式以系統管理員身分執行(UAC)[Windows 權限]
資料來源:http://www.dotblogs.com.tw/as15774/archive/2012/02…
GITHUB: https://github.com/jash-git/Console_CS_Admin
步驟:新增-應用程式資訊清單檔案(app.manifest)
然後看到有一行
<requestedExecutionLevel level=”asInvoker” uiAccess=”false” />
將改成這樣
<requestedExecutionLevel level=”requireAdministrator” uiAccess=”false” />
app.manifest(實際完整檔案)
<?xml version="1.0" encoding="utf-8"?> <asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> <security> <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3"> <!-- UAC 資訊清單選項 如果您要變更 Windows 使用者帳戶控制層級,請將 requestedExecutionLevel 節點取代為下列其中一項。 <requestedExecutionLevel level="asInvoker" uiAccess="false" /> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> <requestedExecutionLevel level="highestAvailable" uiAccess="false" /> 指定 requestedExecutionLevel 節點會停用檔案和登錄虛擬化。 如果您要針對回溯相容性使用檔案及登錄虛擬化, 請刪除 requestedExecutionLevel 節點。 --> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> </requestedPrivileges> </security> </trustInfo> <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> <application> <!-- 可配合此應用程式使用的所有 Windows 版本的清單。Windows 會自動選取最相容的環境。--> <!-- 如果您的應用程式是設計成配合 Windows 7 運作,請取消註解下列 supportedOS 節點--> <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>--> </application> </compatibility> <!-- 啟用 Windows 通用控制項和對話方塊的佈景主題 (Windows XP 以後版本) --> <!-- <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*" /> </dependentAssembly> </dependency>--> </asmv1:assembly>
專案程式碼(PS.純粹順便貼上)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; /* 讓C#程式以系統管理員身分執行(UAC) 資料來源:http://www.dotblogs.com.tw/as15774/archive/2012/02/02/67563.aspx 新增-應用程式資訊清單檔案(app.manifest) 然後看到有一行 <requestedExecutionLevel level="asInvoker" uiAccess="false" /> 將改成這樣 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> */ namespace Console_CS_Admin { class Program { static void ExecuteCommand(string command) { int exitCode; ProcessStartInfo processInfo; Process process; processInfo = new ProcessStartInfo("cmd.exe", "/c " + command); processInfo.CreateNoWindow = true; processInfo.UseShellExecute = false; // *** Redirect the output *** processInfo.RedirectStandardError = true; processInfo.RedirectStandardOutput = true; process = Process.Start(processInfo); process.WaitForExit(); // *** Read the streams *** // Warning: This approach can lead to deadlocks, see Edit #2 string output = process.StandardOutput.ReadToEnd(); string error = process.StandardError.ReadToEnd(); exitCode = process.ExitCode; Console.WriteLine("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output)); Console.WriteLine("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error)); Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand"); process.Close(); } static void Main(string[] args) { ExecuteCommand("123.bat"); Pause(); } static void Pause() { Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } } }
One thought on “讓C#程式以系統管理員身分執行(UAC) [Windows 權限]”
C# UAC 盾牌 Administrator