David Božjak
David Božjak

Reputation: 17607

Install program that has to be run as administrator

Background: I am by no means a windows security / user permissions expert. I have an application (written in C#), that has to be able to write / delete files & folders in its root directory, write / delete files elsewhere on the disk, write/modify values in System Registry (Local Machine) and start & stop other applications and services. I figure that I need administrator privileges for at least some of those actions.

I tried running this and on computers with UAC turned off it works great without any additional settings. However on computers with UAC turned on (any level above 'never notify' in Windows 7) it will crash. I need it to work on all computers.

Up to now I would just manually check the "run this program as administrator" checkbox and everything would be fine. However now we have decided that we will allow customers to install this software on their own, and it needs to run "out of the box".

I have a deployment project in Visual Studio 2008 that installs everything and writes the necessary start up data in registry. What I need to do now is to set the "Run this program as Administrator" flag. I am guessing this isn't quite as simple as I'd like it to be.

So What is the proper way of doing this? This program is started on startup, and it would be irritating for our customers if UAC would pop up (and possibly dim the screen) every time they restart their computer.

Thank you for your help.

EDIT: Thank you for your replies. I realise that working around UAC would be frowned upon, and I can see that Microsoft does not support "white lists" so it would ask for permission only once. That's fine I can respect that, however I do have some follow up questions:

Upvotes: 3

Views: 7485

Answers (3)

blowdart
blowdart

Reputation: 56490

The proper way is to elevate when the program starts, with the UAC prompt (which you can set via the program's manifest) - attempting to be clever and bypass it is frowned upon.

Think about it - if you could install something which would elevate automatically without the UAC prompt ... what would be the point of UAC?

To add a UAC manifest to a program you simply add the manifest in a project and edit it. A sample manifest for UAC is here. If you want to elevate at the last possible moment then you need a spawn separate process - you cannot elevate an existing process. So separate that bit out and then start it using

Process.StartInfo.UseShellExecute = true;
Process.StartInfo.Verb = "runas";

Upvotes: 3

Dirk Vollmar
Dirk Vollmar

Reputation: 176159

You can split your program into two components:

  • a user application running without elevation
  • a Windows service that is responsible for the tasks that require elevation

Since you're using .NET, communication between the components is probably easiest done using WCF.

And as a side note: Programmatically modifying files under C:\Program Files is not considered good practice and might lead to a number of other problems. Windows has dedicated places for storing configuration settings and other program data.

Upvotes: 0

Craig Stuntz
Craig Stuntz

Reputation: 126547

You need to rethink how your application works. You're quite correct that it would be annoying to display an elevation prompt on login. So don't do it. On the other hand, you may well have tasks which you need to perform using administrative access.

So you have two choices:

  • Change your tasks so that they no longer require administrative elevation (e.g., write your files elsewhere).
  • Break your application into a Windows service component and a user interface component. The service component can run under an elevated account (hopefully the least-elevated account necessary to perform the tasks you need to do). The user interface component can talk to the service (via named pipes or similar) when necessary.

Upvotes: 0

Related Questions