I have an application which needs the UAC elevation.
I have the code which lets me give that but the application opens twice.. which is the issue..
so here i
Move the WindowsPrincipal code from your Form to Program.cs as in the example below. This will prompt the user for UAC authority prior to launching any forms and will only launch the form if UAC authority has been granted.
static void Main()
{
WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);
if (!hasAdministrativeRight)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Application.ExecutablePath;
startInfo.Verb = "runas";
try
{
Process p = Process.Start(startInfo);
Application.Exit();
}
catch (System.ComponentModel.Win32Exception ex)
{
MessageBox.Show("This utility requires elevated priviledges to complete correctly.", "Error: UAC Authorisation Required", MessageBoxButtons.OK);
// Debug.Print(ex.Message);
return;
}
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}