Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File.Move does not inherit permissions from target directory?

Tags:

In case something goes wrong in creating a file, I've been writing to a temporary file and then moving to the destination. Something like:

        var destination = @"C:\foo\bar.txt";         var tempFile = Path.GetTempFileName();         using (var stream = File.OpenWrite(tempFile))         {             // write to file here here         }          string backupFile = null;         try         {             var dir = Path.GetDirectoryName(destination);             if (!Directory.Exists(dir))             {                 Directory.CreateDirectory(dir);                 Util.SetPermissions(dir);             }              if (File.Exists(destination))             {                 backupFile = Path.Combine(Path.GetTempPath(), new Guid().ToString());                 File.Move(destination, backupFile);             }              File.Move(tempFile, destination);              if (backupFile != null)             {                 File.Delete(backupFile);             }         }         catch(IOException)         {             if(backupFile != null && !File.Exists(destination) && File.Exists(backupFile))             {                 File.Move(backupFile, destination);             }         } 

The problem is that the new "bar.txt" in this case does not inherit permissions from the "C:\foo" directory. Yet if I create a file via explorer/notepad etc directly in the "C:\foo" there's no issues, so I believe the permissions are correctly set on "C:\foo".

Update

Found Inherited permissions are not automatically updated when you move folders, maybe it applies to files as well. Now looking for a way to force an update of file permissions. Is there a better way overall of doing this?

like image 357
Joseph Kingry Avatar asked May 28 '10 14:05

Joseph Kingry


People also ask

How do I get permission to move a file?

Right click the folder/drive, click on properties, go to the security tab and click on Advanced and then click on the Owner tab. Click edit and then click the name of the person you want to give ownership to (you may need to add it if it isn't there - or it may be yourself).

How do I move a folder and keep permissions?

To preserve permissions when files and folders are copied or moved, use the Xcopy.exe utility with the /O or the /X switch. The object's original permissions will be added to inheritable permissions in the new location.

What happens to the permissions of a file if you move the file to a folder within the same volume?

By default, an object inherits permissions from its parent object, either at the time of creation or when it is copied or moved to its parent folder. The only exception to this rule occurs when you move an object to a different folder on the same volume. In this case, the original permissions are retained.


1 Answers

Found what I needed was this:

var fs = File.GetAccessControl(destination); fs.SetAccessRuleProtection(false, false); File.SetAccessControl(destination, fs); 

This resets the file permissions to inherit.

like image 108
Joseph Kingry Avatar answered Nov 17 '22 15:11

Joseph Kingry