How to set a folder to Full Rights permission in Windows 7

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".
 
Windows7.gif
 
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.
 
Windows7.1.gif
 
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.
 
  Windows7.2.gif
 
In the following window, choose Add Custom Action from the Commit node.
 
Windows7.3.gif
 
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.
 
Windows7.4.gif
 
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.
 
Windows7.5.gif
 
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:
  1. private void SetAccessRights(string file) {    
  2.  FileSecurity fileSecurity = File.GetAccessControl(file);    
  3.  AuthorizationRuleCollection rules = fileSecurity.GetAccessRules(truetruetypeof(NTAccount));    
  4.  foreach(FileSystemAccessRule rule in rules) {    
  5.   string name = rule.IdentityReference.Value;    
  6.   if (rule.FileSystemRights != FileSystemRights.FullControl) {    
  7.    FileSecurity newFileSecurity = File.GetAccessControl(file);    
  8.    FileSystemAccessRule newRule = new FileSystemAccessRule(name, FileSystemRights.FullControl, AccessControlType.Allow);    
  9.    newFileSecurity.AddAccessRule(newRule);    
  10.    File.SetAccessControl(file, newFileSecurity);    
  11.   }    
  12.  }    
  13. }    
The core class in which the whole functionality included is: AccessRightsProvider.cs
 
Windows7.6.gif
 

Summary

 
In this article, we have seen the user access rights issue associated with setup installation in Windows 7 and the solution.


Similar Articles