Setting default permissions in folder

BlearyEye
BlearyEye used Ask the Experts™
on
I'm using C# 4.0. I have a folder "C:\Users\leal\AppData\Roaming\MyApp". The permissions on this folder let Everyone have full access to it. My program creates a file "MyDB.bak" in that folder. When I try to run a restore from SQL Server, I get the error

Cannot open backup device 'C:\Users\leal\AppData\Roaming\Myapp\MyDB.bak'. Operating system error 5(Access is denied.).

The problem is that Everyone does not have access to this file. I know I could explicitly give permissions to MyDB.bak, but I'd prefer to set it up so that files created in the folder inherit the folder's security attributes. Can I do this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Am I missing something? That link doesn't seem to address my issue of causing a new file to inherit the permissions of the parent folder.I know how to apply permissions to a folder or a file; I just don't want to have to do it every time I create a new file in a folder that's exposed to Everyone.
Check the following link.  This might help.

http://stackoverflow.com/questions/2165114/net-folder-permission-issue

Also you could retrieve the Directory or Folder Access Control List using GetAccessControl

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

And apply the directory settings to your created file using SetAccessControl

http://msdn.microsoft.com/en-us/library/system.io.file.setaccesscontrol.aspx

Assuming you haven't created "Everyone" in Folder yet.  You can do it something like in my quick sample code below.  I also provided a link for the FileSystemAccessRule.  I hope this helps.

http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesystemaccessrule.aspx
DirectorySecurity dirSec;
dirSec = new DirectorySecurity();

dirSec = Directory.GetAccessControl(dirPath);

dirSec.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, (InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit), PropagationFlags.InheritOnly, AccessControlType.Allow));

FileSecurity.SetAccessControl(filename, dirSec);

Open in new window

Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Try using the following code snippet :
public static void GiveDirFullPermissionEveryoneDotNet(String dir)
{
    GiveDirFullPermissionDotNet(dir, new String[] { @"TODOS", @"EVERYONE", @"BUILTIN/Users", @"Users", @"NT AUTHORITY\NETWORK SERVICE", @"NETWORK", @"Administrators", @"Administrator", @"Administradores", @"Administrador", @"SYSTEM" });
}

public static void GiveDirFullPermissionDotNet(String dir, String[] users)
{
    DirectorySecurity dirSec = Directory.GetAccessControl(dir);
    FileSystemAccessRule fsar;

    foreach (String userAtual in users)
    {
        try
        {
            fsar = new FileSystemAccessRule(userAtual
                                          , FileSystemRights.FullControl
                                          , InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit
                                          , PropagationFlags.InheritOnly
                                          , AccessControlType.Allow);
            dirSec.AddAccessRule(fsar);
        }
        catch (Exception)
        {
            continue;
        }
    }

    Directory.SetAccessControl(dir, dirSec);
}

Open in new window

@starlite551:

This is exactly the same as what is provided in the link that I posted in my previous post.  This was written at the start.

http://stackoverflow.com/questions/2165114/net-folder-permission-issue

@starlite551:

FYI, the asker was asking this -----> but I'd prefer to set it up so that files created in the folder inherit the folder's security attributes. Can I do this?

The code snippet you provided and in the link only adds additional users using AddAccessRule to the Directory but not to a File as the asker was trying to ask based on my understanding of his question and that is why I did not emphasized the code in my previous post.
Here's what worked, following Alfred1's sample. It appears that I need both of the rules shown: the first for the folder itself and the second to inherit properties for new files. Now new files automatically take on the access properties of the parent.

An odd side effect is that the folder became shared (I'm using Vista). Any idea why that might be happening?
dirSec.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));

dirSec.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, (InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit), PropagationFlags.InheritOnly, AccessControlType.Allow));

Directory.SetAccessControl(appDataPath, dirSec);

Open in new window

Author

Commented:
I didn't see anything here about sharing happening when setting security. Did I miss something?
Tom KnowltonWeb developer

Commented:
I've requested that this question be deleted for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Author

Commented:
even tho no follow-up answer, i can assign points for the main question ...

Author

Commented:
i had to work out a few details but alfred1's answer was basically OK

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial