Link to home
Create AccountLog in
Avatar of BlearyEye
BlearyEyeFlag for United States of America

asked on

Setting default permissions in folder

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?
Avatar of Salim Fayad
Salim Fayad
Flag of Lebanon image

Avatar of BlearyEye

ASKER

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.
SOLUTION
Avatar of Alfred A.
Alfred A.
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
I didn't see anything here about sharing happening when setting security. Did I miss something?
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.
even tho no follow-up answer, i can assign points for the main question ...
i had to work out a few details but alfred1's answer was basically OK