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         }

示例代码

相关推荐
czhc11400756636 小时前
7.29:树形;
c#
cm04Z9c916 小时前
C#摸鱼实录——IoC与DI案例详解
开发语言·c#
数据知道8 小时前
Windows 安全基线:组策略、UAC、Defender 深度配置
windows·安全·网络安全
大模型码小白11 小时前
在 Windows 下 Codex 安装、配置与使用详细指南
java·人工智能·windows
小小龙学IT12 小时前
C++ Placement New 与显式析构:手动对象生命周期管理的艺术
c++·windows·mfc
geovindu12 小时前
CSharp:Chain of Responsibility Pattern
开发语言·后端·设计模式·c#·.net·责任链模式·行为模式
luyun02020212 小时前
论坛里的小工具,吾爱出品
运维·服务器·windows
老王生涯14 小时前
rust开发环境配置-Windows & GNU
开发语言·windows·rust
nLif14 小时前
进程管道通讯-伪终端方式
c++·windows
颜x小14 小时前
[C#]泛型类与泛型方法
开发语言·c++·c#