如何在c#中获得系统帐户权限或运行具有系统规则的应用程序

本文关键字:运行 权限 有系统 应用程序 规则 系统 | 更新日期: 2023-09-27 18:19:24

我想从注册表System'CurrentControlSet'Enum'USB中删除部分,但我不能删除PROPERTIES (windows10)

string user = Environment.UserDomainName + "''" + Environment.UserName;
RegistrySecurity mSec = new RegistrySecurity();
RegistrySecurity mSec = new RegistrySecurity();
RegistryAccessRule newRule =
new RegistryAccessRule(
user,
RegistryRights.FullControl,
InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,
PropagationFlags.None,
AccessControlType.Allow);
RegistryKey hkusb = hklm.OpenSubKey(RegistryPath, RegistryKeyPermissionCheck.ReadSubTree);
RegistrySecurity security = hkusb.GetAccessControl();
if (ContainsRule(security.GetAccessRules(true, true, typeof(SecurityIdentifier)), newRule))
{
Console.WriteLine("Access");
Console.ReadKey();
return;
}
security.AddAccessRule(newRule);
hkusb.SetAccessControl(security); <----error here
hkusb.Close();
showKeyTree(hkusb, "+");
Console.ReadKey();

这段代码允许我删除除PROPERTIES

以外的所有节

我知道我需要得到一个系统权限来做到这一点,但我不知道如何使它在代码。注。我需要把过程作为系统来启动如PsExec,但在代码

如何在c#中获得系统帐户权限或运行具有系统规则的应用程序

  1. 以管理员权限在新的模拟线程中启动此代码:link
  2. 使用管理员权限模拟当前线程:link