Reputation: 2686
In WinForms application when my application is started then the User Account Control dialogbox comes(Only in Windows7).
Can anyone suggest me how programatically I can avoid this - That is my application should start always in admin mode or is there any option to stop this dialogbox from coming?
I am developing in C#,VS2008.
Upvotes: 3
Views: 6812
Reputation: 7
You can simply Remove the UAC prompt by writing two line of code,
string UAC_key = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System";
Registry.SetValue(UAC_key, "EnableLUA", 0);`
Upvotes: 0
Reputation: 21
If you're targeting Windows Vista and your application requires administrator priviledges (such as accessing the program files directory), then it will fail unless you include a manifest so Windows knows. Fortunately, it's very simple.
First, add a manifest file to the root of your .NET executable project - you can do this by selecting "Add New Item" and picking "Application Manifest File". Alternatively just create a blank file called "app.manifest". The standard template that Visual Studio gives you looks like this:
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"
xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC Manifest Options
If you want to change the Windows User Account Control level replace the
requestedExecutionLevel node with one of the following.
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
If you want to utilize File and Registry Virtualization for backward
compatibility then delete the requestedExecutionLevel node.
-->
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</asmv1:assembly>
The key part is the requestedExecutionLevel mode - which by default is set to "asInvoker". This means that the application will run under the priviledges of whoever started the application - remembering that under UAC even administrators on windows Vista are running as Users until they elevate their permissions.
If your application always requires administrator priviledges, then you can change this value to "requireAdministrator". Now, whenever your application starts it will always trigger UAC and ask the user to allow administrator access for your program.
Note that it's worth seriously considering whether you actually need to do this, and why - especially given the intrusive experience of UAC. For instance, you shouldn't need to write settings to Program Files, as Windows provides the user profile area and registry for just that purpose. In general UAC should hopefully force us all to think a bit more carefully about where we're storing data, and what permissions the application really needs.
Upvotes: 2
Reputation: 556
You cannot automatically start an application with higher privileges without accepting the prompt or telling Windows to always start that application with higher privileges. Being able to programatically change this behaviour would defeat the whole purpose of UAC. UAC comes up automatically if it's active and an application does something that requires higher privileges than the current user. Being an administrator user does not give you the rights to do everything without confirmation first, which actually only then elevates your privileges if UAC is active.
What you should do is track down what actually triggers UAC in your application and remove that or modify it so that the application does not need higher privileges to successfully complete that task. You could also check if the manifest of your application requests higher privileges, check for requestedExecutionLevel in the manifest. In case you actually do need higher privileges your only way is to either tell the users to turn off UAC which still does not mean a "normal" user is able to run your application. Or to always accept the prompt when starting your application.
Upvotes: 10