How to Give Read/Write Permissions to a Folder During Installation Using .Net

How to give Read/Write permissions to a Folder during installation using .NET

By default Users group doesn't have write access in per-machine locations like Program Files. This is a Windows standard which is not related to installations. However, during install you can set any permissions you want.

Windows Installer does support custom permissions, but Visual Studio doesn't offer a way for setting them. So the only solution in Visual Studio is a custom action.

Unfortunately Visual Studio doesn't support attached custom actions. So using XCACLS.EXE to set permissions would work only if you include it in your package (it will be installed on the target machine along with your files).

A cleaner, but more complex solution is to write a custom action yourself (using custom code) to set the permissions you want.

The fastest and cleanest solution would be to use a different setup authoring tool which offers more control over permissions.

Set write permissions for all users for my program folder

Perhaps the best answer isn't what you've asked for. There's a good reason for not writing to the program files directory. Log data in particular is transient and shouldn't be written here.

It's a much better idea to write log data to the directory specified by the TEMP environment variable. If you do this you'll save your users a few troubles and prevent them cursing your software in the future. Please check out this answer which covers the same topic:

Allow access permission to write in Program Files of Windows 7

How to give Create, Read and Modify permissions to all users for a file created by my application in C:\ProgramData?

I would use a folder in the Environment.SpecialFolder enum.

Example:

var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

How do I grant permission for the application folder to full access during installation in a target machine?

You could do one of the following:

You could modify your application manifest to run the application as administrator.

<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

OR

You could place the mdb file in folder other than Program Files.
Typically files that need write access in an application are placed in

C:\Users\<User Name>\AppData\Local\<Manufacturer>\<Product>\<Product Version> 

To get the Application Data folder path, use the following:

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


Related Topics



Leave a reply



Submit