开发者

Programmatically adding Write permissions to files in WPF

开发者 https://www.devze.com 2023-03-27 13:56 出处:网络
In my WPF MVVM application,I have a XML file to modify. It is successfully working In Visual Studio. But It showing error, while running the installed application.

In my WPF MVVM application,I have a XML file to modify. It is successfully working In Visual Studio. But It showing error, while running the installed application. How can i set the Permissions Through code..

me used this code ,

// current security settings.
FileSecurity fSecurity = File.GetAccessControl(FilePath);

// Add the FileSystemAccessRule to the security settings.
string rr = WindowsIdentity.GetCurrent().Name;
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..

see the exception...

System.UnauthorizedAccessException: Attempted to perform an unauthorized operation. at System.Security.AccessControl.Win32.SetSecurityInfo(ResourceType type, String name, SafeHandle handle, SecurityInfos securityInformation, SecurityIdentifier owner, SecurityIdentifier group, GenericAcl sacl, GenericAcl dac开发者_如何学Pythonl) at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, SafeHandle handle, AccessControlSections includeSections, Object exceptionContext) at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, AccessControlSections includeSections, Object exceptionContext) at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, AccessControlSections includeSections) at System.Security.AccessControl.FileSystemSecurity.Persist(String fullPath) at System.IO.File.SetAccessControl(String path, FileSecurity fileSecurity)


To set permissions mostly it requires power user rights(assuming your are running windows 7).

To verify above, launch the Visual studio as "Run as Administrator" and debug the your code.

What is exact exception message?

Here is working example: It sets full permissions for user group EveryOne

private static void WriteAcl ( string filename ) 
        { 
            //Set security for EveryOne Group
            SecurityIdentifier sid =new SecurityIdentifier(WellKnownSidType.WorldSid, null);
            IdentityReference userIdentity =sid.Translate (typeof(NTAccount));           

            var AccessRule_AllowEveryOne = new FileSystemAccessRule ( userIdentity, FileSystemRights.FullControl, AccessControlType.Allow );
            var securityDescriptor = new FileSecurity ();
            securityDescriptor.SetAccessRule ( AccessRule_AllowEveryOne );            
            File.SetAccessControl ( filename, securityDescriptor ); 
        } 

This code works only if User Account Settings are set to Never notify. Looks its turned on at your computer?

A workaround is to launch your application as power user using Application Manifest.

http://msdn.microsoft.com/en-us/library/bb756929.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消