Overview

If a application has privileged access to file system (e.g. application runs under administrator account) a directory or file can't be edited by a non privileged user any more. The following snippet will give the access to filesystem (newly generated files or directories) to any user in the system. The file or directory have to exist before snippet will work.

If a application has privileged access to file system (e.g. application runs under administrator account) a directory or file can't be edited by a non privileged user any more. The following snippet will give the access to filesystem (newly generated files or directories) to any user in the system. The file or directory have to exist before snippet will work.

Snippet

        public static void DirectoryForAllUsers(string targetDirectory)
        {
            var info = new DirectoryInfo(targetDirectory);
            var allUsersSid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
            var security = info.GetAccessControl();
            security.AddAccessRule(new FileSystemAccessRule(allUsersSid, FileSystemRights.FullControl, AccessControlType.Allow));
            info.SetAccessControl(security);
        }
		
        public static void DirectoryForAllUsers(string targetDirectory)
        {
            var info = new DirectoryInfo(targetDirectory);
            var allUsersSid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
            var security = info.GetAccessControl();
            security.AddAccessRule(new FileSystemAccessRule(allUsersSid, FileSystemRights.FullControl, AccessControlType.Allow));
            info.SetAccessControl(security);
        }