Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I copy security information when creating a new folder?

In my app I'm creating folders for archiving old stuff from a harddisc.

When creating a new folder I must copy all NTFS rights (Groups / Users) from the source folder to the newly created destination folder.

Here is what I've written so far:

FileSecurity fileSecurity =
    File.GetAccessControl(filenameSource, AccessControlSections.All);
FileAttributes fileAttributes = File.GetAttributes(filenameSource);
File.SetAccessControl(filenameDest, fileSecurity);
File.SetAttributes(filenameDest, fileAttributes);

Is this really all I ought to do or am I missing something important?

like image 345
dhh Avatar asked Nov 05 '22 13:11

dhh


1 Answers

If that is a folder, then you may would want to check Directory.SetAccessControl() Method. You may would want to call DirectorySecurity.SetAccessRuleProtection(isProtected/*true*/,preserveInheritance /*false*/) if you want to prevent the files from inheriting ACL rules, before calling Directory.SetAccessControl();

like image 99
KMån Avatar answered Nov 15 '22 05:11

KMån