C#开启和关闭UAC功能


在开发软件或制作安装包时,有时会需要管理员权限 ,但是又不想弹出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         }

 

示例代码

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM