29

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?

1
  • I had same issue. I never quite figure out the cause. However, note that File.Copy does NOT have the same problem. (Something about ownership??)
    – aampere
    Jan 27 at 2:40

1 Answer 1

40

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.

5
  • 3
    Do you have to do this once the file has already been moved? In which case it's no longer atomic - there's a risk someone could try and read the file before the permissions are in place
    – Neil
    Apr 24, 2012 at 12:03
  • 1
    @Neil yes, this would be after the move and yes it would make it no longer atomic. Apr 25, 2012 at 13:27
  • 3
    @JosephKingry. Thanks, this helped me out. But, I also wanted to remove any explicit permissions as a result of the Move. Just a few more lines of code. stackoverflow.com/a/12821819/486660
    – Jim
    Oct 10, 2012 at 18:57
  • Is this also necessary when copying a file or are the permissions automatically inherited in that case? Jul 2, 2017 at 22:21
  • It seems File.Copy does not have the same problem. Neil it seems ridiculous, but copying the file then deleting the original might be a solution.
    – aampere
    Jan 27 at 2:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.