Noam Gal
Noam Gal

Reputation: 3405

Check if user has write permission in "Program Files"

I need to check whether the current user has write permissions inside the Program Files folder.
The main problem occurs in Vista/7 - If I just try to create a temp file in this location I get an exception, even if the user can perform such an operation using the Windows Explorer (After allowing UAC elevation). Sounds reasonable, as the process itself was not ran with administrator privileges.
I then tried using this solution, but I always got back "true", even when I tried running it with a standard (non-admin) user.

What I eventually want to be able to answer, is in case the user tries to create a directory inside the Program Files, would he need to supply an administrator credentials, or would a simple click on "continue" in the UAC suffice?
I am looking for a way to answer this question without raising the UAC pop-up (of any kind) myself. Is there a relevantly easy way to do it?

UPDATE

Thanks for offering me to use the UserData folder, but I just need to know if the user has write access in the folder so I can decide whether or not I should try to perform an auto-update (which runs an msi), or not.

Upvotes: 2

Views: 2502

Answers (3)

Dirk Vollmar
Dirk Vollmar

Reputation: 176269

There are good reasons that the ProgramFiles location does not allow write access without user consent. You would have to either

  1. run your application always elevated (asking for user consent each time it is started), or
  2. use a service for auto-updating
  3. download the update to the user's temp folder and then run the update with elevation

Option 1 and 2 have serious drawbacks, so I recommend to go for option 3. This way is used by other applications as well (e.g. FireFox).

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273844

It would be better to stick to the rules. Note that not all users may be able to elevate through UAC. And you could try to write and it mays seem to succeed but with Vista/Win7 'virtualizing' your write somewhere else.

Since you marked this C# and Windows, take a look at

string dataPath = 
   Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

Upvotes: 3

Veeti
Veeti

Reputation: 5310

A better solution is to just store application data where you're supposed to:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

Upvotes: 6

Related Questions