Take the 2-minute tour ×

Well I noticed that on Windows 7, sometimes even when you are an administrator, you can't do a lot of things, probably it's some sort of bug, my application I check if an user is administrator before start the program because my program creates file in folders that are protected default like the root folder ( C: ), and if you aren't an administrator on Windows 7, you can only create folders there.

So if I right click in my application and go "Run as Administrator", it just works fine. Is there a way to make my application run as administrator automatically? I would like to be able to make a line of code like: ActivateAdministrator(); and be available for the code completely, because I change attributes, create files with ifstream.

share|improve this question
I think the problem is: If there was a way to do that, then malware would be doing it to get admin privileges. –  Mysticial Apr 21 '12 at 0:01
The folder restrictions and UAC popups are there for a reason. If you found a way round them, M$ would rush out a 'Critical Security Update' and your code would stop working again. –  Martin James Apr 21 '12 at 0:04
I don't think he's trying to circumvent the UAC, I believe he wants to elevate privileges of his application normally by whatever the default OS has setup (UAC enabled or disabled). –  Erik Philips Apr 21 '12 at 0:08
Yea I'm sorry, now I see how my question sounded for you guys, what I meant is that I want that once my program is asked to run, it requires administrator to proceed. –  Grego Apr 21 '12 at 11:35
If the only reason your program needs administrator privilege is to create folders in protected locations, why don't you just create the folders somewhere else? –  Harry Johnston Apr 22 '12 at 0:03
show 1 more comment

3 Answers

up vote 4 down vote accepted

You could add a manifest to your executable - http://msdn.microsoft.com/en-us/library/bb756929.aspx

If the user is running on a system with the UAC switched on, and are not an administrator, a manifest which contains requestedExecutionLevel level="requireAdministrator" will produce a prompt for the Administrator password before your application can run with administrative privileges. (requiring administrator privileges means that an incorrect password or no password will stop it from running altogether)

If they are an administrator with the UAC switched on, then that same manifest will cause a Yes/No prompt to ask whether your application should be granted administrative privileges.

Of course, the real issue is that whatever your application is doing which requires administrative privileges needs to be examined.

Most of the time the privilege is simply not required for normal user-level applications. This is an application design issue really - what is your application doing which requires admin privileges? is it really necessary? e.g. If you're modifying files, then why are those files in a protected area on the file system instead of in the user's profile space?

share|improve this answer
add comment

Although this is in C#, it might be easier for you I don't know. What I did was to C#: Detect if running with elevated privileges?, and if not rerun the current process while requesting administrative access (which if the UAC is enabled, would do a popup to the current user and ask if it is ok for the program to run with administrative privleges).

Then some simple (but C# code) looks like:

// UAC is a class from the previous link on SO
if (UAC.IsCurrentProcessElevated())
  string currentProcess = Assembly.GetEntryAssembly().Location;
  string arguments = string.Join(" ", this._Args.ToArray());
  ProcessStartInfo startInfo = new ProcessStartInfo(currentProcess, arguments); 
  startInfo.UseShellExecute = true;
  startInfo.Verb = "runas";

The un-elevated process would quite, with a new one started that requested administrative privileges.

share|improve this answer
add comment

Your Answer


