7

I'm trying to backup files on a server, but some of them are in use and cannot be opened. Instead, I'd like to open their shadow copy if the current copy is in use. How can I do this?

For reference, I'm using C# .net 3.5.

Malfist
  • 31,179
  • 61
  • 182
  • 269

3 Answers3

9

This question is quite old already, so my answer might not be of much use to you, but SO being a Q&A site maybe it still helps someone else.

I can't / don't want to put down the entire implementation, but the procedure is goes something like this:

  1. You create a Volume Shadow Copy via the Volume Shadow Service Provider for the drive where your file to be read is located(this is well documented on MSDN, also there is a sample client that creates these shadow copies and will most likely be sufficient for you)

  2. Either make a persistent one, or use the "callback" mechanism (calls your app)

  3. Open the desired file via UNC paths and CreateFile (the UNC looks something like this: \\?\GlobalRoot\Devices\HarddiskVolumeShadowCopyXZY\yourpath\yourfile.yourextension)

  4. Do whatever you want with the file

  5. If you made a persistent VSC you should use the sample client to delete it after you're done

more info here: http://technet.microsoft.com/en-us/library/cc785914%28WS.10%29.aspx and here: http://msdn.microsoft.com/en-us/library/bb968832%28VS.85%29.aspx

Tom
  • 3,115
  • 6
  • 33
  • 38
  • 2
    The problem is that .NET's built-in file management tools cannot access \\?\GlobalRoot directives. You actually have to drop down to the winapi level. There is a library called AlphaFSS which can, but it has some major problems. I used it for a while but ran into problems with the file size required both DWORD's to represent it's size, AlphaFSS only reads the lower DWORD. – Malfist Sep 08 '10 at 14:15
  • I know this is an old thread... but .Net 4.62 and above does support the \\?\ prefix – egray Oct 17 '18 at 15:12
2

I cannot actually tell, but there is the following Channel 9 video.

Windows Vista "Time Warp": Understanding Vista's Backup and Restore Technologies

There are some implementation details and a bit about the API structure. And I believe to remember that they mentioned how the shadow copies are mapped into the file system.

Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143
2

If you have control of the first process you can specify file handle share type

string contents1;
string contents2;
using (FileStream fs1 = new FileStream("test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    using (var tr1 = new StreamReader(fs1))
    {
        using (FileStream fs2 = new FileStream("test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            using (var tr2 = new StreamReader(fs2))
            {
                contents2 = tr2.ReadToEnd();
                contents1 = tr1.ReadToEnd();
            }
        }
    }
}

Console.WriteLine(contents1);
Console.WriteLine(contents2);
Sergej Andrejev
  • 9,091
  • 11
  • 71
  • 108