views:

1788

answers:

3

I have created an application that writes some data to the root folder of the directory in which it was installed (in Program Files). But under Windows Vista, the program is unable to write to the directory because the UAC restricts administrative privileges.

I need to be able to do the following

  1. Write a file in the folder where the program was installed in program files.

That's possible if the software is run with administrative privileges. But I don't know how to modify my setup to always run it with administrative privileges.

Are there any ways or suggestions I can accomplish this?

A: 

Your best bet is to not write any files under "Program Files". Whatever you're trying to write there is better written to the Windows Registry. You will then have to of course modify your program to read the information from the Registry instead of that file. But really, this is what the Registry is for.

JoelFan
If you don't know what this file is, how do you know it belongs in the registry? What if it's a config file (preferred over the registry)?
GalacticCowboy
+1  A: 

I thought Vista handled this for programs that "looked like setup programs." Are you using an installer like NSIS or Inno? I think Inno has a way to ask for this. Not sure though.

:EDIT: From the inno help on admin/ [Setup] privileges required section:

Valid values: none, poweruser, or admin
Default value: admin

Description: The effect of this directive depends on which version of Windows the user is running:

On Windows Vista and later:

This directive affects whether elevated rights are requested (via a User Account Control dialog) when the installation is started.

When set to admin (the default) or poweruser, Setup will always run with administrative privileges. If Setup was started by an unprivileged user, Windows will ask for the password to an account that has administrative privileges, and Setup will then run under that account.

When set to none, Setup will only run with administrative privileges if it was started by a member of the Administrators group. Do not use this setting unless you are sure your installation will run successfully on unprivileged accounts.

On Windows NT/2000/XP/2003:

This directive specifies the minimum user privileges required to run the installation.

When set to admin (the default), Setup will only run if the user is a member of the Administrators group. Otherwise, it will display the following message and exit: "You must be logged in as an administrator when installing this program."

When set to poweruser, Setup will only run if the user is a member of the Administrators or Power Users groups. Otherwise, it will display the following message and exit: "You must be logged in as an administrator or as a member of the Power Users group when installing this program."

When set to none, Setup will not check the user's group membership. Do not use this setting unless you are sure your installation will run successfully on unprivileged accounts.

On Windows 95/98/Me:

This directive has no effect on these versions of Windows.

Tim
+1  A: 

It sounds like the title of your question asks how to make the installer run with administrative privileges but your explanation asks how to make the program run with administrative privileges so it can write to Program Files at runtime. Which one is it?

If you really just want the installer to run with administrative privileges, the community wiki answer is pretty accurate. Make sure your installer has an obvious name like "Setup.exe" and Vista uses "heuristics" to decide your installer must run as admin.

If you really want to write to Program Files at runtime, don't. This is the official advice for Vista: write to the user's AppData directory instead. If you have way too much code to rewrite, you can include a manifest file that forces Vista to prompt for administrative rights when running your app. If your app is named "app.exe," include a "app.exe.manifest" file that contains the following:

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestversion="1.0">
<assemblyidentity version="1.0.0.0" processorarchitecture="X86" name="app.exe" type="win32">
<description>My Application</description>
<trustinfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedprivileges>
<requestedexecutionlevel level="requireAdministrator">
</requestedexecutionlevel>
</requestedprivileges>
</security>
</trustinfo>
flipdoubt