C# Process - Disable User Account Control prompt?

末鹿安然 提交于 2019-12-24 12:23:51

问题


Question Background:

I am calling a batch file to run a specified dll against the MSTest.exe through the use of a Process object.

The code:

I want to ensure that at all times this process is run with elevated administrator permissions.

I do this currently by setting the process verb property to runas, as shown:

 try
 {
     _process.StartInfo.Verb = "runas";
     _process.StartInfo.CreateNoWindow = true;
     _process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
     _process.StartInfo.Arguments = testLocation + " " + testDll;
     _process.StartInfo.FileName = batchFileLocation;
     _process.Start();
     _process.WaitForExit();
 }
 catch (Exception ex)
 {
     throw ex;
 }

The issue:

This runs as expected but the User Account Control (UAC) prompt asks to confirm that I wish the process to run the .exe.

As this code is part of an an automated process I cannot have any human input to click the 'Yes' button on the prompt, how can I disable the UAC prompt?


回答1:


You cannot bypass the UAC prompt.

Ask yourself: What would you do if you were doing this on Windows XP?

Imagine you were running on a operating system that did not have UAC. What would your dll do then? You no longer have the convenience of UAC, and instead you are always running as a standard user. What would your test regime do then?

  • crash?
  • fail the test?

If you don't like the UAC dialog you can turn it off. But you'll be in a no better situation, because now you're just like you were on Windows XP: a standard user and no easy way to get out of it.

The Real Solution

The real solution is to fix what your dll is doing that is protected. For example, if this is a COM dll, and the self-registration is failing because you are trying to write to HKLM, then you need to

  • grant yourself Full Control to the HKLM\Software\Classes node

If your dll is crashing because you are trying to write to C:\Program Files, then you need to change the security settings on C:\Program Files to give yourself access.

The solution is to fix what is failing.



来源:https://stackoverflow.com/questions/24887142/c-sharp-process-disable-user-account-control-prompt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!