在開發軟件或制作安裝包時,有時會需要管理員權限 ,但是又不想彈出UAC對話框。
可以編寫一個小工具,檢測UAC是否關閉。如果沒有關閉,就自動關閉UAC。
實現比較簡單,
找到注冊表
計算機\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System下的EnableLUA值,改為0。默認是1。
C#實現代碼如下
1 private bool DisableUAC() 2 { 3 try 4 { 5 string path = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"; 6 string uac = "EnableLUA"; 7 RegistryKey key = Registry.LocalMachine.CreateSubKey(path); 8 if (key != null) 9 { 10 key.SetValue(uac, 0, RegistryValueKind.DWord); 11 key.Close(); 12 } 13 14 return true; 15 } 16 catch(Exception ex) 17 { 18 MessageBox.Show(ex.Message); 19 return false; 20 } 21 } 22 23 private void Reboot() 24 { 25 System.Diagnostics.Process.Start("shutdown", " -r -t 0"); 26 }