Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my WPF MVVM application,I have a XML file to modify. It is successfully working In Visual Studio. But Its not working, while running the installed application. How can i set the Permissions Through code..

me used this code ,
C#
// current security settings. FileSecurity fSecurity = File.GetAccessControl(FilePath);

        // Add the FileSystemAccessRule to the security settings.
       
        fSecurity.AddAccessRule(new FileSystemAccessRule(WindowsIdentity.GetCurrent().Name,
            FileSystemRights.FullControl, AccessControlType.Allow));


        // Set the new access settings.
        File.SetAccessControl(FilePath, fSecurity);

Still cant solve the Problem...,

Thanks in advance..
Posted
Updated 9-Aug-11 20:30pm
v2

1 solution

Hello
You may have to set the InheritanceFlags as well.

Example:
C#
DirectorySecurity dirsec = null;
DirectoryInfo dir = new DirectoryInfo(@"C:\temp");
String identity = "SomeUsername";

dirsec = dir.GetAccessControl(AccessControlSections.Access);

rule = new FileSystemAccessRule(identity, FileSystemRights.FullControl | FileSystemRights.Synchronize, AccessControlType.Allow);

dirsec.AddAccessRule(rule);

rule = new FileSystemAccessRule(identity, FileSystemRights.FullControl | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);

dirsec.AddAccessRule(rule);

dir.SetAccessControl(dirsec);


Also ensure that the user/account running the application has the privileges to set access control.
If you get an exception, feel free to post it :-)

I hope that helps...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900