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

Related Questions

Sponsored Content

17 Answered Questions

[SOLVED] Copying files from Docker container to host

46 Answered Questions

[SOLVED] Deep cloning objects

  • 2008-09-17 00:06:27
  • NakedBrunch
  • 782818 View
  • 2164 Score
  • 46 Answer
  • Tags:   c# .net clone

16 Answered Questions

[SOLVED] How to clone or copy a list?

16 Answered Questions

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

25 Answered Questions

[SOLVED] Case insensitive 'Contains(string)'

16 Answered Questions

[SOLVED] open() in Python does not create a file if it doesn't exist

22 Answered Questions

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

22 Answered Questions

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

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

17 Answered Questions

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

Sponsored Content