0

I need to create a file in the path c:/Progran File (x86). But it throws a error saying access denied. I have tried the below code, but it didn't help.

   private bool GrantAccess(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);
      return true;
   }       
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
VJL
  • 359
  • 1
  • 5
  • 10
  • You are running in admin mode right? – Loufs Jul 27 '16 at 14:38
  • 1
    For writing to `c:/Progran File (x86)` normally administrator privileges are required. If you want to know how to run as Admin have a look here: http://stackoverflow.com/a/2818776/1466583 – Maximilian Ast Jul 27 '16 at 14:38
  • you don't have permissions to write on Program Files – meJustAndrew Jul 27 '16 at 14:38
  • 1
    Why do you want to write to Program Files folder? You really shouldn't do that, use `Environment.SpecialFolder.ApplicationData` instead. – Timo Salomäki Jul 27 '16 at 14:40
  • I am not running in admin mode. I still I want to create file in path c:/Program File (x86) – VJL Jul 27 '16 at 14:41
  • @VJL but you need the admin privileges to write to that path. Just open a explorer and try to create a file/folder. Even there a admin request window will be prompt. [What makes c:\Program Files UAC-protected?](http://stackoverflow.com/questions/16617004/what-makes-c-program-files-uac-protected) – Maximilian Ast Jul 27 '16 at 14:47
  • You probably can not. Program Files are often blocked for non admin access. As a side note, all machinations changing the rules defining non admin access to Program Files require admin privileges to be edited themselves. So it's probably a dead end. – Sergey.quixoticaxis.Ivanov Jul 27 '16 at 14:56

0 Answers0