private void StartInVscSession() { string sourceVolume = PathHelper.RemoveTrailingSeparator(Path.GetPathRoot(SourceFolder)); _vscSession = new VolumeShadowCopySession(); _vscSession.Error += VscSession_Error; _vscSession.Ready += VscSession_Ready; _status.OnEnterNewStage("Preparing...", string.Format("Creating shadow copy of volume {0} ...", PathHelper.Quote(sourceVolume))); // create and mount the shadow copy _vscSession.Start(SourceFolder); _status.IsAbortingSupported = true; }
private void Main() { string sourceFolder = PathHelper.AppendSeparator(_session.SourceFolder); // C:\, C:\Folder\, \\Server\Share\, \\Server\Share\Folder\ string volume = PathHelper.RemoveTrailingSeparator(Path.GetPathRoot(sourceFolder)); // C:, C:, \\Server\Share, \\Server\Share string pathFromRoot = PathHelper.RemoveTrailingSeparator(sourceFolder.Substring(volume.Length)); // "", \Folder, "", \Folder if (pathFromRoot.Length == 0) { pathFromRoot = null; // null, \Folder, null, \Folder } try { // helper function throwing an ObjectDisposedException if _disposeEvent is currently signaled Action CheckForDisposeRequest = () => { if (_disposeEvent.WaitOne(0)) { throw new ObjectDisposedException("VscThread"); } }; CheckForDisposeRequest(); _backup = VssUtils.LoadImplementation().CreateVssBackupComponents(); _backup.InitializeForBackup(null); _backup.SetContext(VssSnapshotContext.Backup); _backup.SetBackupState(false, false, VssBackupType.Copy, false); CheckForDisposeRequest(); _backup.GatherWriterMetadata(); CheckForDisposeRequest(); _snapshotSetID = _backup.StartSnapshotSet(); _volumeSnapshotID = _backup.AddToSnapshotSet(PathHelper.AppendSeparator(volume)); CheckForDisposeRequest(); _backup.PrepareForBackup(); VerifyWriterStatus(); CheckForDisposeRequest(); // create the snapshot _backup.DoSnapshotSet(); VerifyWriterStatus(); // mount the source folder as file share // (non-persistent shadow copies cannot be mounted locally on a drive or folder) string shareName = _backup.ExposeSnapshot(_volumeSnapshotID, pathFromRoot, VssVolumeSnapshotAttributes.ExposedRemotely, null); _session._mountPoint = @"\\localhost\" + shareName; CheckForDisposeRequest(); } catch (ObjectDisposedException) // by CheckForDisposeRequest() { Cleanup(); return; } catch (Exception e) { Cleanup(); string msg = (e is UnauthorizedAccessException ? "You lack the required privileges to create a volume shadow copy.\nPlease restart DeeMirror as administrator." : "The volume shadow copy could not be created:\n\n" + e.Message); _session.OnError(msg); return; } // fire the Ready event try { _session.OnReady(); } // on unhandled exception by Ready event handler: Cleanup() before rethrowing catch { Cleanup(); throw; } // wait for Dispose() signal _disposeEvent.WaitOne(); Cleanup(); }