patterncsharpMajor
Administration Elevation
Viewed 0 times
administrationelevationstackoverflow
Problem
I'm looking into Administration Elevation and I've come up with a solution that seems like it's perfectly sane, but I'm still in the dark about the professional methods to accomplish this.
Is there a better way to do this or is this fine?
```
using System;
using System.Diagnostics;
using System.Security.Principal;
using System.Windows.Forms;
namespace MyVendor.Installation
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (!IsRunAsAdmin())
{
Elevate();
Application.Exit();
}
else
{
try
{
Installer InstallerForm = new Installer();
Application.Run(InstallerForm);
}
catch (Exception e)
{
//Display Exception message!
Logging.Log.Error("Unrecoverable exception:", e);
Application.Exit();
}
}
}
internal static bool IsRunAsAdmin()
{
var Principle = new WindowsPrincipal(WindowsIdentity.GetCurrent());
return Principle.IsInRole(WindowsBuiltInRole.Administrator);
}
private static bool Elevate()
{
var SelfProc = new ProcessStartInfo
{
UseShellExecute = true,
WorkingDirectory = Environment.CurrentDirectory,
FileName = Application.ExecutablePath,
Verb = "runas"
};
try
{
Process.Start(SelfProc);
return true;
}
catch
{
Logging.Log.Error("Unable to elevate!");
return false;
}
}
}
}
Is there a better way to do this or is this fine?
```
using System;
using System.Diagnostics;
using System.Security.Principal;
using System.Windows.Forms;
namespace MyVendor.Installation
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (!IsRunAsAdmin())
{
Elevate();
Application.Exit();
}
else
{
try
{
Installer InstallerForm = new Installer();
Application.Run(InstallerForm);
}
catch (Exception e)
{
//Display Exception message!
Logging.Log.Error("Unrecoverable exception:", e);
Application.Exit();
}
}
}
internal static bool IsRunAsAdmin()
{
var Principle = new WindowsPrincipal(WindowsIdentity.GetCurrent());
return Principle.IsInRole(WindowsBuiltInRole.Administrator);
}
private static bool Elevate()
{
var SelfProc = new ProcessStartInfo
{
UseShellExecute = true,
WorkingDirectory = Environment.CurrentDirectory,
FileName = Application.ExecutablePath,
Verb = "runas"
};
try
{
Process.Start(SelfProc);
return true;
}
catch
{
Logging.Log.Error("Unable to elevate!");
return false;
}
}
}
}
Solution
You can create a manifest file and set the app to require administrative privileges. This will trigger the UAC user prompt with the dimmed screen when your application is run without requiring any code on your part.
See MSDN for the gory details:
This file can be created by using any text editor. The application manifest file should have the same name as the target executable file with a .manifest extension.
See MSDN for the gory details:
This file can be created by using any text editor. The application manifest file should have the same name as the target executable file with a .manifest extension.
"
type="win32"/>
Description of your application
Context
StackExchange Code Review Q#197, answer score: 22
Revisions (0)
No revisions yet.