0

I've tried the snippet of code:

                    Process p = new Process();
                    ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
                    info.UseShellExecute = true;
                    info.Verb = "runas";
                    p.StartInfo = info;
                    p.Start();

                    using (StreamWriter sw = p.StandardInput)
                    {
                        if (sw.BaseStream.CanWrite)
                        {
                            sw.WriteLine("DISM.exe /Online /Add-Capability /CapabilityName:Microsoft.WebDriver~~~~0.0.1.0");
                        }
                    }

It successfully opens up a command prompt with administrative privileges but I can't seem to write into it use streamwriter. I know that this is because I don't have RedirectStandardInput set to true. If I do set this to true, I am able to write but my console loses its elevated privileges. My question is how can I write a command into the console when UseShellExecute is true?

vfirma
  • 330
  • 1
  • 5
  • 15
  • 1
    If your process doesn't have an administrative user token, there's no way it can write to a process that *does* have an administrative user token. If it could, UAC would be broken and pointless. – madreflection Mar 06 '20 at 20:01
  • Makes sense. What approach would you propose? – vfirma Mar 06 '20 at 20:04
  • 1
    Run your process as administrator first. If your code works *without* elevating cmd.exe, that's the only way you can do it *with* elevating it. – madreflection Mar 06 '20 at 20:11
  • Have you tried passing the command to run in the CMD window using `cmd.exe`'s `/k` option (instead of using a stream). This, of course, won't solve your UAC issue – Flydog57 Mar 06 '20 at 20:15
  • I've included a manifest in my project with but it doesn't seem to be taking effect – vfirma Mar 06 '20 at 20:19

0 Answers0