By WeaslB


2012-02-06 16:51:51 8 Comments

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't lose the permissions?

1 comments

@Alex Mendez 2012-02-06 17:01:49

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);

@WeaslB 2012-02-06 17:29:16

Works like a charm! Thanks!

@Mantosh Kumar 2014-11-14 06:58:55

Thanks for the above information.

Related Questions

Sponsored Content

11 Answered Questions

[SOLVED] What is the difference between the 'COPY' and 'ADD' commands in a Dockerfile?

  • 2014-07-25 14:31:20
  • Steve
  • 582060 View
  • 1918 Score
  • 11 Answer
  • Tags:   docker dockerfile

22 Answered Questions

[SOLVED] Copy the entire contents of a directory in C#

  • 2008-09-12 11:38:52
  • Keith
  • 317115 View
  • 488 Score
  • 22 Answer
  • Tags:   c# .net copy

15 Answered Questions

[SOLVED] Copying files from Docker container to host

15 Answered Questions

[SOLVED] How to clone or copy a list?

45 Answered Questions

[SOLVED] Deep cloning objects

  • 2008-09-17 00:06:27
  • NakedBrunch
  • 760202 View
  • 2096 Score
  • 45 Answer
  • Tags:   c# .net clone

16 Answered Questions

[SOLVED] How do I copy a file in Python?

24 Answered Questions

[SOLVED] Case insensitive 'Contains(string)'

11 Answered Questions

[SOLVED] How to copy a folder from remote to local using scp?

18 Answered Questions

[SOLVED] How can I exclude all "permission denied" messages from "find"?

21 Answered Questions

[SOLVED] How do I copy an object in Java?

Sponsored Content