Editing registry on non-admin user (c#, windows 11)

huangapple go评论60阅读模式
英文:

Editing registry on non-admin user (c#, windows 11)

问题

I'm using the following code to disable task manager:

RegistryKey regKey;
string subKey = @"Software\Microsoft\Windows\CurrentVersion\Policies\System";

regKey = Registry.CurrentUser.CreateSubKey(subKey);
regKey.SetValue("DisableTaskMgr", 1);
regKey.Close();

My program is running with admin rights.
When I run it on a non-admin user (I enter the password of an admin user to run it), it doesn't work (the registry key is created successfully, but task manager isn't disabled).
When I run it on an admin user, it does work.

Does anyone have a solution for this?

英文:

I'm using the following code to disable task manager :

RegistryKey regKey;
string subKey = @"Software\Microsoft\Windows\CurrentVersion\Policies\System";

        
regKey = Registry.CurrentUser.CreateSubKey(subKey);
regKey.SetValue("DisableTaskMgr", 1);
regKey.Close();

My program is running with admin rights.
When I run it on non-admin user (I enter the password of an admin user to run it), it doesn't work (the registry key is created successfully, but task manager isn't disabled).
When I run it on admin user, it does work.

Does anyone have a solution for this?

答案1

得分: 1

我已解决问题,感谢@RaymondChen的评论。

ManagementObjectSearcher searcher = new ManagementObjectSearcher(
    "SELECT UserName FROM Win32_ComputerSystem");

ManagementObjectCollection collection = searcher.Get();
string username = (string)collection.Cast<ManagementBaseObject>().First()["UserName"];
NTAccount account = new NTAccount(username);
SecurityIdentifier securityIdentifier = (SecurityIdentifier)account.Translate(typeof(SecurityIdentifier));
string sid = securityIdentifier.ToString();

string subKey = $@"{sid}\Software\Microsoft\Windows\CurrentVersion\Policies\System";

RegistryKey regKey = Registry.Users.CreateSubKey(subKey);
regKey.SetValue("DisableTaskMgr", 1);
regKey.Close();
英文:

I have solved the issue thanks to @RaymondChen comment.

ManagementObjectSearcher searcher = new ManagementObjectSearcher(
            &quot;SELECT UserName FROM Win32_ComputerSystem&quot;);

        ManagementObjectCollection collection = searcher.Get();
        string username = (string)collection.Cast&lt;ManagementBaseObject&gt;().First()[&quot;UserName&quot;];
        NTAccount account = new NTAccount(username);
        SecurityIdentifier securityIdentifier = (SecurityIdentifier)account.Translate(typeof(SecurityIdentifier));
        string sid = securityIdentifier.ToString();

        string subKey = $@&quot;{sid}\Software\Microsoft\Windows\CurrentVersion\Policies\System&quot;;

        RegistryKey regKey = Registry.Users.CreateSubKey(subKey);
        regKey.SetValue(&quot;DisableTaskMgr&quot;, 1);
        regKey.Close();

huangapple
  • 本文由 发表于 2023年4月4日 05:58:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定