C#开启和关闭UAC功能

在开发软件或制作安装包时,有时会需要管理员权限 ,但是又不想弹出UAC对话框。

可以编写一个小工具,检测UAC是否关闭。如果没有关闭,就自动关闭UAC。

实现比较简单,

找到注册表

计算机HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionPoliciesSystem下的EnableLUA值,改为0。默认是1

C#实现代码如下

 1  private bool DisableUAC()
 2         {
 3             try
 4             {
 5                 string path = @"SOFTWAREMicrosoftWindowsCurrentVersionPoliciesSystem";
 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         }

示例代码

原文地址:https://www.cnblogs.com/zhaotianff/p/11596668.html