[Solved] "Access denied" when opening a directory with BackupFileStream

Dec 13, 2011 at 4:38 PM
Edited Dec 13, 2011 at 4:55 PM

I'm trying to backup all the stream and security data associated with a directory, but when I try to create a BackupFileStream I get 'access denied'. This is true both when accessing a directory in a volume shadow copy and also when accessing a directory just created in code.

e.g. on the writing side going to an 'ordinary' volume:-


            // Work out full path name
            String lPath = IO.Path.Combine(mRootPath, RootName, RelativePath);

            // Create a DirectoryInfo structure
            var lIODirectory = new FS.DirectoryInfo(lPath);
            // Create the directory (if it doesn't exist)
            if (!lIODirectory.Exists) FS.Directory.CreateDirectory(lPath);

            using (var lStream = new FS.BackupFileStream(lPath, FS.FileMode.Open)) {

                // Write the stream data to it
                int lRead;
                Byte[] lBuf = new Byte[4096];
                while ((lRead = Data.Read(lBuf, 0, 4096)) > 0) {
                    lStream.Write(lBuf, 0, lRead, true);


This is running as an elevated service, with System.Security.Principal.IsInRole(WindowsBuiltInRole.Administrator) showing as true for the process owner.

Is this a sensible approach to copying stream data and security, or an I just misreading the whole thing about using BackupFileStream and the corresponding wrapped API calls for files and directories.  (Edit) A call to copy file data later in code does appear to work as expected.

Any clues, hints or examples gratefully received.


Edit: OpenOrCreate  files doesn't complain, but the code gets 'access denied' on the first write using the stream

Dec 14, 2011 at 11:50 AM

Apologies for replying to my own post - it turns out I'm stupid.

I had wanted to read the security information, so the original constructor included a third parameter for file system rights of FS.FileSystemRights.SystemSecurity. Unfortunately it didn't include useful things like Read or Write, hence access denied. By the time I posted the above query the write side of things had lost the extra parameter, but the read side hadn't, so I was still getting access denied on the read. 

Dec 18, 2011 at 3:37 AM

No worries :)

Thanks for finding it useful.