views:

91

answers:

3

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.

+4  A: 

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

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
Veeti
+2  A: 

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);
Henk Holterman
A: 

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).

0xA3
The update will run with elevated permissions, naturally. I do not expect the user to install an msi without first acknowledging it.All I want to do is know in advance whether the user HAS write permission inside "Program Files" or not. I need this knowledge so I can make an informed decision if I want to use my auto update feature or not.To make it clear one more time - will the user be asked to give admin credentials during the msi run, or would he simply need to click on "continue"?
Noam Gal
It depends on whether or not the user is an administrator. Testing for write permissions on Program Files is not the way to check for this; instead, you should check if the user is a member of the Administrators group.
Luke
group permission may change (A user can be in the "Administrators" group, which does not have any special permission on the computer), or not in this group, but granted specific permission on the folder (or even in another group "Foo", that have such permission).I want to be %100 sure the user can write to the "Program Files". Is it possible?
Noam Gal
If you really want to do that the only way is AccessCheck (http://msdn.microsoft.com/en-us/library/aa374815.aspx), but it is kind of a pain to use. I'm not sure if there is a .NET equivalent.
Luke