Introduction
Often, developers receive access rights errors while installing
application in Windows 7 and Vista.
The error message will typically look like "Access to the path is denied".
The same setup executed in a Windows XP machine will work fine, so the
reason behind the above error is probably due to User Access Control changes in the Windows
7 and Vista operating systems. The changes are documented in MSDN.
You can view the user access level after installing the application.
You can see that the Full Control, Modify, and Write permissions are unchecked for
the logged-in user. In this case, any attempt to modify the file will throw an exception.
Solution
We can approach the problem by using a customer installer action. After
committing the installation, a custom executable is launched, which will take
care of providing all permissions to the file.
The new executable could be a console application which will perform the
following:
- Find the current execution folder
- Find all files in the folder recursively
- Set Full Access Rights to all the files
The application is named as SetAccessRights.exe.
Modifying the Setup Application
For calling the above SetAccessRights.exe, we need to set custom actions in the
setup application. For this, right-click on the setup and choose Custom Actions.
In the following window, choose Add Custom Action from the Commit node.
In the appearing dialog box, select the Primary output from the SetAccessRights
file. Then select the added file and change the property InstallerClass to false
as shown below.
Now build the setup and execute it. After installation, run the FolderRights.exe
from the installed folder in Program Files. Click the Write button on the
executable and this time no error message will be shown.
You can open the File.txt and see the appended data.
Code Attached
The attached source code contains the main application, setup application,
access rights setter application. You can execute the setup and see the results.
The core method providing full access rights is shown below:
- private void SetAccessRights(string file) {
- FileSecurity fileSecurity = File.GetAccessControl(file);
- AuthorizationRuleCollection rules = fileSecurity.GetAccessRules(true, true, typeof(NTAccount));
- foreach(FileSystemAccessRule rule in rules) {
- string name = rule.IdentityReference.Value;
- if (rule.FileSystemRights != FileSystemRights.FullControl) {
- FileSecurity newFileSecurity = File.GetAccessControl(file);
- FileSystemAccessRule newRule = new FileSystemAccessRule(name, FileSystemRights.FullControl, AccessControlType.Allow);
- newFileSecurity.AddAccessRule(newRule);
- File.SetAccessControl(file, newFileSecurity);
- }
- }
- }
The core class in which the whole functionality included is:
AccessRightsProvider.cs
Summary
In this article, we have seen the user access rights issue associated with setup
installation in Windows 7 and the solution.