Search code examples
c#windowsregistryadministratortaskmanager

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 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?


Solution

  • I have solved the issue thanks to @RaymondChen comment.

    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();