0

I used this Code to grant my application full read/write access about a path:

 private void GetAccess(string fullPath)
 {
     DirectoryInfo dInfo = new DirectoryInfo(fullPath);
     DirectorySecurity dSecurity = dInfo.GetAccessControl();
     dSecurity.AddAccessRule(new FileSystemAccessRule(new
          SecurityIdentifier(WellKnownSidType.WorldSid, null),
          FileSystemRights.FullControl, InheritanceFlags.ObjectInherit |
          InheritanceFlags.ContainerInherit,
          PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
     dInfo.SetAccessControl(dSecurity);           
 }

But when it comes to dInfo.SetAccessControl(dSecurity) it throws an UnauthorizedAccessException although i have full administrative rights at the computer i'm using.

Is there a way to grant this access through another method or give my application any access in advance to process this command?

Alex
  • 21,273
  • 10
  • 61
  • 73
fl0s3n
  • 1
  • 2
  • 1
    Your user may have permissions, but does the active process? – christophano Jan 26 '16 at 15:31
  • @christophano no, my application doesn't have, that's what i want to change because this tool will generate an error log and write in it, but without write permissions for the folder that contains the log file i can't get it to work – fl0s3n Jan 26 '16 at 16:21
  • Well there's the problem - you can't change the permissions because the application doesn't have the necessary authority. – christophano Jan 26 '16 at 17:27
  • @christophano i know that much so far, do you have any suggestion to grant my application this access? – fl0s3n Jan 27 '16 at 13:34
  • Is this an interective application? Win Forms, Wpf? – christophano Jan 27 '16 at 14:00
  • It's an Windows Forms application, and i solved it a much simpler way, at first i just created a Streamwriter that creates my log file when it's not already created. Now my application creates the file before it's functional part with the Streamwriter starts and my problem is gone. – fl0s3n Jan 28 '16 at 09:16
  • That's one way I was going to suggest you do it. The other was to specify the execution level in the application manifest. Glad you're sorted though! – christophano Jan 28 '16 at 09:19

1 Answers1

0

In Windows World, Admin privilege doesn't mean all the files/folders are directly open to users with admin right. In order to access files not owned by your account and you don't have an access, you may need to take the ownership of file/folder.

Please refer to the following question/answer

Getting / setting file owner in C#

Community
  • 1
  • 1
Jay Kim
  • 63
  • 3
  • I tried to change the owner of this folder and the same problem as described in my question occurs, i can't manage to programmically change any access permissions. – fl0s3n Jan 26 '16 at 16:17