我正在创建将修改Windows服务的应用程序。但由于某些原因,它不会让我运行“cs config SERVICE_NAME set = SETTING”或修改注册表以更改服务的启动设置,因为需要管理员。 我已经在管理员帐户上给了它全部管理权限。无法修改C#中的注册表(或运行管理员级命令)作为管理员
不管我做什么,它总是会通过一个错误,说它无法访问注册表,或者它不会使用CMD进行设置,因为它没有权限。它被卡在被称为“DCOM服务器进程启动”
特定服务下面是安全设置我的应用程序的清单
<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>
这使得它的管理权限启动时的程序。我甚至手动右键单击文件并选择“以管理员身份运行”,但它仍然无法工作。
要进入注册表,这是代码,它还包含CMD过程。
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;
这里的CMD进程代码:
//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);