12

I am trying to run an exe file from another user account name, it shows following error

    System.ComponentModel.Win32Exception: The requested operation requires an elevation
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start()
    at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)

Here is my code

ProcessStartInfo pro = new ProcessStartInfo(application);
pro.UseShellExecute = false;
pro.Verb = "runas";
pro.WorkingDirectory = workingdirectory;
pro.RedirectStandardInput = true;
pro.RedirectStandardOutput = true;
pro.CreateNoWindow = true;

Process process = Process.Start(pro);

How to resolve this?

user3797438
  • 405
  • 3
  • 6
  • 24

1 Answers1

19

Unfortunately, you cannot do

  • run with elevated permissions and
  • redirect input/output

simultaneously.

Reason:

  • Verb is only recognized when UseShellExecute = true, but
  • redirecting IO requires UseShellExecute = false.

More information:

I guess in your situation you will have to skip using runas, but rather ensure that your application is already started with the correct user account/permissions. This should work, since processes started by elevated processes "inherit" elevation.

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • I have to run with elevated permissions only ....i dont want to redirect input/output...can u suggest me the code..@Heinzi – user3797438 Sep 01 '14 at 07:25
  • There's a way to write in the manifest that an application should always run as an admin, right? I don't remember how because it's been a while, but that'd probably be a good thing to do for this. I'm sure there are places documenting it. – Matthew Haugen Sep 01 '14 at 07:26
  • @user3797438: If you don't want to redirect input/output, why do you set `RedirectStandardInput` to `true`? Surely, every line in your code has a good reason for being there, right? – Heinzi Sep 01 '14 at 07:45