0

I want my application not to need an Admin to use it.

I should be usable for a normal user.

When a user wants to change some settings of the app I need Admin Rights.

So I want to launch a second instance of the application which requires Admin Rights. (with user agreement request and so on)

Is there any way to accomplish that?

I tried:

Process p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.FileName = Application.ExecutablePath;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
if (System.Environment.OSVersion.Version.Major >= 6)
{
    p.StartInfo.Verb = "runas";
}
p.Start();

But it seems not to work.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Neuxz
  • 311
  • 1
  • 4
  • 12
  • I don't think an un-elevated permission process can start an elevated permission process – Yaman Apr 12 '18 at 13:32
  • Possible duplicate of [Elevating process privilege programmatically?](https://stackoverflow.com/questions/133379/elevating-process-privilege-programmatically) – Ricardo Pontual Apr 12 '18 at 13:33

2 Answers2

1

You can to create an account with admin rights,

Then populate the user,password properties on the ProcessStartInfo instance with the corresponding values for the admin account.

Something like:

var psi = new ProcessStartInfo
{
    FileName = "notepad.exe",
    UserName = "admin",
    Domain = "",
    Password = pass,
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardError = true
};
Process.Start(psi);
Evyatar
  • 1,107
  • 2
  • 13
  • 36
0

Turns out i have to use the UseShellExecute in the ProcessStartInfo and not the Process. More explanation here.

            ProcessStartInfo proc = new ProcessStartInfo();
            proc.UseShellExecute = true;
            proc.WorkingDirectory = Environment.CurrentDirectory;
            proc.FileName = Application.ExecutablePath;
            proc.Verb = "runas";

            try
            {
                Process.Start(proc);
            }
            catch
            {
                return; //U have a BIG problem!
            }
Neuxz
  • 311
  • 1
  • 4
  • 12