Windows 7 and Vista Uac - Programmatically Requesting Elevation in C#

Windows 7 and Vista UAC - Programmatically requesting elevation in C#

WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
bool hasAdministrativeRight = principal.IsInRole(WindowsBuiltInRole.Administrator);

if (!hasAdministrativeRight)
{
RunElevated(Application.ExecutablePath);
this.Close();
Application.Exit();
}

private static bool RunElevated(string fileName)
{
//MessageBox.Show("Run: " + fileName);
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.Verb = "runas";
processInfo.FileName = fileName;
try
{
Process.Start(processInfo);
return true;
}
catch (Win32Exception)
{
// Do nothing. Probably the user canceled the UAC window
}
return false;
}

Windows 7 UAC elevation

No, elevation is per process, not thread.

If the rest of the application has to run non-elevated, you could run yourself elevated with some parameter (myapp.exe /uac "ipcparamhere") and use some sort of Inter-process communication to communicate back to the "main instance" of your app. (If the elevated process only performs a simple operation, you could probably check for success by using the exit code of the process)

How to prevent Vista from requiring elevation on patch.exe?

From:

http://social.msdn.microsoft.com/Forums/en-US/windowsgeneraldevelopmentissues/thread/bf4f7dfa-5553-41d3-9c8e-311ee4a88599/

If you can add a manifest to the
affected executable declaring a
requestedExecutionLevel of 'asInvoker'
it should stop prompting.

Associated guide on UAC architecture and converting existing applications so they work correctly (near the bottom fifth of the page):

http://technet.microsoft.com/en-us/library/cc709628.aspx

Lastly, how to write such a manifest:

http://www.google.com/search?q=writing+a+uac+manifest

-Adam

Creating folders in UAC-protected areas

This is usually done by creating a separate process with the "runas" verb:

  • ShellExecute example
  • C# example

This process can perform any operations which require elevation.

Vista UAC - permissions and certificates for an exe

By signing the installer, the yellow UAC warning dialog will change its color to gray and the text will be like:

A program needs your permission to continue

If you started this program, continue

Is UAC on Win7 different than Vista?

Larry Osterman in the comment for another answer provides the answer.

There are differences but they're
extremely minor (mostly related to the
auto-elevation logic which prevents a
number of prompts). One user-visible
difference is that applications that
have been marked as requiring the
Windows XP appcompat layer will now
require elevation. – Larry Osterman 2
days ago



Related Topics



Leave a reply



Submit