-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Snapshot.cs
105 lines (91 loc) · 3 KB
/
Snapshot.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#region Copyright Notice
/*
* AlphaVSS Sample Code
* Written by Jay Miller
*
* This code is hereby released into the public domain, This applies
* worldwide.
*/
#endregion
using System;
using Alphaleonis.Win32.Vss;
namespace FolderSync
{
/// <summary>
/// Utility class to manage the snapshot's contents and ID.
/// </summary>
class Snapshot : IDisposable
{
/// <summary>A reference to the VSS context.</summary>
IVssBackupComponents _backup;
/// <summary>Metadata about this object's snapshot.</summary>
VssSnapshotProperties _props;
/// <summary>Identifier for the overall shadow copy.</summary>
Guid _set_id;
/// <summary>Identifier for our single snapshot volume.</summary>
Guid _snap_id;
/// <summary>
/// Initializes a snapshot. We save the GUID of this snap in order to
/// refer to it elsewhere in the class.
/// </summary>
/// <param name="backup">A VssBackupComponents implementation for the current OS.</param>
public Snapshot(IVssBackupComponents backup)
{
_backup = backup;
_set_id = backup.StartSnapshotSet();
}
/// <summary>
/// Dispose of the shadow copies created by this instance.
/// </summary>
public void Dispose()
{
try { Delete(); } catch { }
}
/// <summary>
/// Adds a volume to the current snapshot.
/// </summary>
/// <param name="volumeName">Name of the volume to add (eg. "C:\").</param>
/// <remarks>
/// Note the IsVolumeSupported check prior to adding each volume.
/// </remarks>
public void AddVolume(string volumeName)
{
if (volumeName.StartsWith(@"\\?\")) //roland: IsVolumeSupported does not like \\?\ paths
volumeName = volumeName.Substring(4);
if (!volumeName.EndsWith(@"\")) //roland: IsVolumeSupported requires \ at the end
volumeName += @"\";
if (_backup.IsVolumeSupported(volumeName))
//if (true)
_snap_id = _backup.AddToSnapshotSet(volumeName);
else
throw new VssVolumeNotSupportedException(volumeName);
}
/// <summary>
/// Create the actual snapshot. This process can take around 10s.
/// </summary>
public void Copy()
{
_backup.DoSnapshotSet();
}
/// <summary>
/// Remove all snapshots.
/// </summary>
public void Delete()
{
_backup.DeleteSnapshotSet(_set_id, false);
}
/// <summary>
/// Gets the string that identifies the root of this snapshot.
/// </summary>
public string Root
{
get
{
if (_props == null)
_props = _backup.GetSnapshotProperties(_snap_id);
return _props.SnapshotDeviceObject;
}
}
}
}