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
No Console Output When Using Allocconsole and Target Architecture X86
What Is the "Right" Way to Bring a Windows Forms Application to the Foreground
How Does Foreach Work When Looping Through Function Results
Getting Specified Node Values from Xml Document
Use Reflection to Invoke an Overridden Base Method
Win32_Processor::Is Processorid Unique for All Computers
How to Get the Last Five Characters of a String Using Substring() in C#
How to Use Httpclient to Send Content in Body of Get Request
What's the Best Strategy for Equals and Gethashcode
Why Isn't Arraylist Marked [Obsolete]
String Concatenation VS String Builder. Performance
How to Save Image in Database Using C#
Why Appdomain.Currentdomain.Basedirectory Not Contains "Bin" in ASP.NET App
C# - Fill a Combo Box with a Datatable
.Net (C#): Getting Child Windows When You Only Have a Process Handle or Pid