Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Copying a Folder to another destination with Security/Permission Settings

I am making a program where it could copy a Folder and Transfer it to another location, including the attribute, permissions, security settings.

So Far I got the Attribution to work, but am having problems with the permissions/security settings. Here are my code:

Directory.CreateDirectory(Destination);
DirectoryInfo DestAttribute = new DirectoryInfo(Destination);
DestAttribute.Attributes = Source.Attributes; // Copies Attributes from Source to Dest

AuthorizationRuleCollection Rule;
DirectorySecurity DestSecurity = Source.GetAccessControl();
Rule = DestSecurity.GetAccessRules(true, true, typeof(NTAccount));
DestSecurity.AddAccessRule(Rule);
DestAttribute.SetAccessControl(DestSecurity);

Anyone have any suggestion on getting this to work ? Thank you everyone for all the help.

like image 814
FerX32 Avatar asked Jul 18 '12 03:07

FerX32


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

MSDN: How To copy ACL information from one file to another

The SetAccessControl method persists only DirectorySecurity objects that have been modified after object creation. If a DirectorySecurity object has not been modified, it will not be persisted to a file. Therefore, it is not possible to retrieve a DirectorySecurity object from one file and reapply the same object to another file.

To copy ACL information from one file to another:

  1. Use the GetAccessControl method to retrieve the DirectorySecurity object from the source file.

  2. Create a new DirectorySecurity object for the destination file.

  3. Use the GetSecurityDescriptorBinaryForm or GetSecurityDescriptorSddlForm method of the source DirectorySecurity object to retrieve the ACL information.

  4. Use the SetSecurityDescriptorBinaryForm or SetSecurityDescriptorSddlForm method to copy the information retrieved in step 3 to the destination DirectorySecurity object.

  5. Set the destination DirectorySecurity object to the destination file using the SetAccessControl method.

Example:

DirectoryInfo dir1 = new DirectoryInfo(@"C:\Temp\Dir1");
DirectoryInfo dir2 = new DirectoryInfo(@"C:\Temp\Dir2");  


DirectorySecurity ds1 = dir1.GetAccessControl();
DirectorySecurity ds2 = new DirectorySecurity();
ds2.SetSecurityDescriptorBinaryForm(ds1.GetSecurityDescriptorBinaryForm());
dir2.SetAccessControl(ds2);
like image 193
Faisal Mansoor Avatar answered Oct 07 '22 14:10

Faisal Mansoor


This appears to be a duplicate of:

Original Question...

(code sample from original question)

FileInfo file1 = new FileInfo(@"c:\test.txt");
FileInfo file2 = new FileInfo(@"c:\test2.txt");
StreamReader sr1 = new StreamReader(file1.Open(FileMode.Open));
StreamWriter sw1 = new StreamWriter(file2.Open(FileMode.Create));
sw1.Write(sr1.ReadToEnd());
sr1.Close();
sw1.Close();
FileSecurity ac1 = file1.GetAccessControl();
ac1.SetAccessRuleProtection(true, true);
file2.SetAccessControl(ac1);

I put together the following method and it appears to do what you want...

private static void FolderCopy(String sourceFolder, String destinationFolder)
{
    DirectoryInfo sourceDirectory = new DirectoryInfo(sourceFolder);
    DirectoryInfo destinationDirectory;

    if (!sourceDirectory.Exists)
    {
        throw new DirectoryNotFoundException("Source folder not found: " + sourceFolder);
    }

    if (!Directory.Exists(destinationFolder))
    {
        destinationDirectory = Directory.CreateDirectory(destinationFolder);
    }
    else
    {
        destinationDirectory = new DirectoryInfo(destinationFolder);
    }

    DirectorySecurity security = sourceDirectory.GetAccessControl();

    security.SetAccessRuleProtection(true, true);
    destinationDirectory.SetAccessControl(security);

    var filesToCopy = sourceDirectory.GetFiles();

    foreach (FileInfo file in filesToCopy)
    {
        String path = Path.Combine(destinationFolder, file.Name);
        FileSecurity fileSecurity = file.GetAccessControl();

        fileSecurity.SetAccessRuleProtection(true, true);

        file.CopyTo(path, false);

        FileInfo copiedFile = new FileInfo(path);

        copiedFile.SetAccessControl(fileSecurity);
    }
}

Chris

like image 35
Chris Keller Avatar answered Oct 07 '22 14:10

Chris Keller