Copy a file with its original permissions

后端 未结 1 1905
逝去的感伤
逝去的感伤 2020-12-17 01:28

When using the File.Copy() method the file is copied to its new directory however it loses its original permissions.

Is there a way to copy a file so that it doesn\'

1条回答
  •  隐瞒了意图╮
    2020-12-17 02:01

    I believe you can do something like this:

    const string sourcePath = @"c:\test.txt";
    const string destinationPath = @"c:\test2.txt"
    
    File.Copy(sourcePath, destinationPath);
    
    FileInfo sourceFileInfo = new FileInfo(sourcePath);
    FileInfo destinationFileInfo = new FileInfo(destinationPath);
    
    FileSecurity sourceFileSecurity = sourceFileInfo.GetAccessControl();
    sourceFileSecurity.SetAccessRuleProtection(true, true);
    destinationFileInfo.SetAccessControl(sourceFileSecurity);
    

    0 讨论(0)
提交回复
热议问题