Solved

how to change file permissions in a .NET 4.0 Setup project

Posted on 2010-08-30
3
477 Views
Last Modified: 2012-05-10
Hello Experts,

I have a setup projects that installs a winForms applications on the users machine. The user has an option to save some defaults in the .config file for the application, but when they try it throws "Access to the path is denied." When I look the folder is "Read".  I can change this manually, but that still doesn't fix the issue. How can I assign the appropriate permissions during my installation to let user update the .config file?
0
Comment
Question by:MDKDevelopers
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
MikeQc earned 500 total points
Comment Utility
It's probably because your application is installed under the "Program Files" folder.
Application settings should normally be changed only by administrators.
By default, regular users does not have access to "Program Files" and his children.

So, that said, if you want to give access to only a specific file in read/write for everyone, you should use this code in your installer:

            string filename = @"C:\Program Files\Your App Folder\Your App.exe.config";  // <-- Change with the correct .config file
            // This will retreive the "Everyone" string depending of your OS version
            SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
            NTAccount acct = sid.Translate(typeof(NTAccount)) as NTAccount;
            string strEveryoneAccount = acct.ToString();
            // Get the current Access Control settings for your file
            FileSecurity filesec = File.GetAccessControl(filename);
            // Add "Everyone" with the "Modify" right
            filesec.AddAccessRule(new FileSystemAccessRule(strEveryoneAccount, FileSystemRights.Modify, AccessControlType.Allow));
            File.SetAccessControl(filename, filesec);

Open in new window

0
 
LVL 6

Expert Comment

by:MikeQc
Comment Utility
-> By default, regular users does not have access to "Program Files" and his children.

I mean does not have "modification" access to "Program Files"...
0
 

Author Closing Comment

by:MDKDevelopers
Comment Utility
I did not get to try out the solution because we opted to go with Click Once deployment. I want to award you the pionts since I believe it's only fair to do so.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now