1

I'm writing a c# program that should ran a proccess and print the output to the console or file. But the exe I want to run must run as admin.
I saw that to run an exe as admin i need to set useShellExecute to true. but to enable output redirection i need to set it to false.

What can i do to achieve both? Thank you!

In this code I get the error printed to the console (because UseShellExecute=false ), and when changed to true - the program stops.

                ProcessStartInfo proc = new ProcessStartInfo();
                proc.UseShellExecute = false;
                proc.WorkingDirectory = Environment.CurrentDirectory;
                proc.FileName = "aaa.exe";
                proc.RedirectStandardError = true;
                proc.RedirectStandardOutput = true;        
                proc.Verb = "runas";

                Process p = new Process();
                p.StartInfo = proc;

                p.Start();        

                while (!p.StandardOutput.EndOfStream)
                {
                    string line = p.StandardOutput.ReadLine();
                    Console.WriteLine("*************");
                    Console.WriteLine(line);        
                }
marco polo
  • 159
  • 1
  • 2
  • 12
  • This might be relevant: http://stackoverflow.com/questions/18660014/redirect-standard-output-and-prompt-for-uac-with-processstartinfo/19381478#19381478 – supertopi Jan 04 '15 at 13:23
  • I didnt understand it very much. Its very long and in c++. And I dont want this question to get anymore complicated. – marco polo Jan 04 '15 at 13:36
  • This is not possible as intended. The process that calls Process.Start() must be elevated itself. Since yours surely isn't, you must write a little helper program that asks for elevation in its manifest. You can start that one without a problem. It in turn can then get the redirection going. – Hans Passant Jan 04 '15 at 13:48
  • I've never tried this, but maybe you could call a batch file that has the output redirection embedded in the DOS command, like `runas theAdminID "myCommand.exe > someFile.txt"`. Then just read the file. – BobRodes Jan 04 '15 at 15:16
  • Thanks for your help, but I found out my solution - at least. I can just run vs as administrator, or ran the program exe as such. – marco polo Jan 04 '15 at 16:21

1 Answers1

0

You could try something like this:

#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
#endregion


namespace CSUACSelfElevation
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }


        private void MainForm_Load(object sender, EventArgs e)
        {
            // Update the Self-elevate button to show a UAC shield icon.
            this.btnElevate.FlatStyle = FlatStyle.System;
            SendMessage(btnElevate.Handle, BCM_SETSHIELD, 0, (IntPtr)1);
        }

        [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, IntPtr lParam);

        const UInt32 BCM_SETSHIELD = 0x160C;


        private void btnElevate_Click(object sender, EventArgs e)
        {
            // Launch itself as administrator
            ProcessStartInfo proc = new ProcessStartInfo();
            proc.UseShellExecute = true;
            proc.WorkingDirectory = Environment.CurrentDirectory;
            proc.FileName = Application.ExecutablePath;
            proc.Verb = "runas";

            try
            {
                Process.Start(proc);
            }
            catch
            {
                // The user refused to allow privileges elevation.
                // Do nothing and return directly ...
                return;
            }

            Application.Exit();  // Quit itself
        }
    }
}
ArchAngel
  • 634
  • 7
  • 16