Kyle Colantonio
Kyle Colantonio

Reputation: 454

Cannot Modify Registry (or run admin level commands) in C# as Admin

I'm creating an application that will modify Windows Services. For some reason however, it won't let me run "cs config SERVICE_NAME set= SETTING" or modify the registry to change the startup setting for a Service since Admin is required. I already gave it full admin access on an admin account.

No matter what I do it will always through an error saying it doesn't have access with the Registry or it won't set it with CMD because it doesn't have permission. The specific Service it gets stuck on is called "DCOM Server Process Launcher"

Here's the security settings in my app's Manifest

<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
 <security>
  <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
    <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
  </requestedPrivileges>
  <applicationRequestMinimum>
    <defaultAssemblyRequest permissionSetReference="Custom" />
    <PermissionSet ID="Custom" SameSite="site" />
  </applicationRequestMinimum>
 </security>
</trustInfo>

This gives it admin rights when it starts up the program. I've even manually right-clicked the file and chose "Run as Administrator" and it still would not work.

To go into the Registry, this is the code and it also contains the CMD Process.

RegistryKey key = null;
key = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\" + service, true); //true should make it Read/Write??
if (key != null)
{
     //cmd.issueCmd("sc config " + service + " start= " + setting); //set a service with CMD
     key.SetValue("Start", val, RegistryValueKind.DWord); //set a service with Registry
     if (setting.Equals("delayed-auto"))
     {
         key.SetValue("DelayedAutoStart", 1, RegistryValueKind.DWord); //Add the Delayed-Start Registry if needed
      }
}
if (key != null)
{
    key.Close();
}
return;

Here's the CMD Process code:

        //Create a Hidden CMD Prompt to issue commands
        ProcessStartInfo processStartInfo = new ProcessStartInfo();
        processStartInfo.RedirectStandardInput = true;
        processStartInfo.RedirectStandardOutput = true;
        processStartInfo.UseShellExecute = false;
        processStartInfo.CreateNoWindow = true;
        processStartInfo.FileName = "cmd.exe";
        processStartInfo.Verb = "runas"; //should give it admin??
        cmd = Process.Start(processStartInfo);

Upvotes: 1

Views: 2014

Answers (1)

Kyle Colantonio
Kyle Colantonio

Reputation: 454

I found the solution to this after a lot of research. I used the example provided from this post on codeproject: http://www.codeproject.com/Articles/7665/Extend-ServiceController-class-to-change-the-Start

After setting it up and changing it to how I needed, everything worked fine for all Windows Operating Systems (XP and newer at least).

Upvotes: 1

Related Questions