Solved

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

Posted on 2010-08-30
3
479 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
ID: 33563310
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
ID: 33563315
-> 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
ID: 33566378
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 our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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