0

When I run dism /Online /Disable-Feature:Microsoft-Hyper-V-Allcommand from command prompt, It works fine.

But same I want to do from C# code. It does not work, Process exists with error code 11.

Process proc = new Process();
proc.StartInfo.FileName = "dism.exe";
proc.StartInfo.Arguments = "/Online /Disable-Feature:Microsoft-Hyper-V-All";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.Verb = "runas";
proc.Start();
proc.WaitForExit();
int exitCode = proc.ExitCode;

Basically I want to run the given command from C# code (with UAC) as it works with command prompt.

MethodMan
  • 18,625
  • 6
  • 34
  • 52
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197

2 Answers2

1

You can't. That is, you as a programmer, don't get to decide whether or not your code runs with administrative rights. However, you can inform the user that your code requires administrative rights and then ask to be granted those rights. How to do that is covered here.

Community
  • 1
  • 1
Necoras
  • 6,743
  • 3
  • 24
  • 45
1

try this method,

You'll want to modify the manifest that gets embedded in the program. This works on Visual Studio 2008 and higher: Project + Add New Item, select "Application Manifest File". Change the element to:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Zaid Mirza
  • 3,540
  • 2
  • 24
  • 40