BlearyEye
asked on
Setting default permissions in folder
I'm using C# 4.0. I have a folder "C:\Users\leal\AppData\Roa ming\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\Roa ming\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?
Cannot open backup device 'C:\Users\leal\AppData\Roa
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?
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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);
}
@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
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.
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Check this out. This might help.
http://social.msdn.microsoft.com/forums/en-US/clr/thread/858a9315-eace-4f3b-a595-f924da4cf3f9
http://social.msdn.microsoft.com/forums/en-US/clr/thread/858a9315-eace-4f3b-a595-f924da4cf3f9
ASKER
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.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
ASKER
even tho no follow-up answer, i can assign points for the main question ...
ASKER
i had to work out a few details but alfred1's answer was basically OK
http://www.redmondpie.com/applying-permissions-on-any-windows-folder-using-c/