tags:

views:

12

answers:

1

Due to the joys of UAC, I need to open an elevated command prompt programmatically and then redirect the standard input so I can use the time command.

I can open the link (a .lnk file) if I use

Process ecp = System.Diagnostics.Process.Start("c:/ecp.lnk");

however, if I use this method, I can't redirect the standardIn.

If I use the StartProcessInformation method (which works wonderfully if you are calling an exe)

ProcessStartInfo processStartInfo = new ProcessStartInfo("c:/ecp.lnk");
                processStartInfo.UseShellExecute = false;
                processStartInfo.ErrorDialog = false;
                processStartInfo.RedirectStandardError = true;
                processStartInfo.RedirectStandardInput = true;
                processStartInfo.RedirectStandardOutput = true;
                Process process = new Process();
                process.StartInfo = processStartInfo;
                bool processStarted = process.Start();

                StreamWriter inp = process.StandardInput;
                StreamReader oup = process.StandardOutput;
                StreamReader errorReader = process.StandardError;
                process.WaitForExit();

I get the error message: The specified executable is not a valid Win32 application.

Can anyone help me create an elevated command prompt which I can capture the standard input of? Or if anyone knows how to programatically escalate a command prompt?

A: 

In case no-one comes up with a better idea (pretty please), here is the work around one of the more devious in my office just came up with:

  1. Copy cmd.exe (the link it pointing at this file)
  2. Paste this file into a different directory
  3. Rename the newly pasted file to something different
  4. Set the permissions on this new file to Run As Administrator

You will still get the escalation dialog popping up, but at least you can capture the standardIn of this valid Win32 app!

Tim Joseph