public void CancelRestorePoint()
        {
            var service = new SystemRestoreTestService();

            try
            {
                var restorePoint = SystemRestorePoint.Create(RestorePointType.ApplicationInstall, service: service);
                restorePoint.Rollback();

                // Tests the default description since it shouldn't show up (cancelled).
                Assert.AreEqual <string>("Windows Installer PowerShell Module", restorePoint.Description);
                Assert.AreEqual <long>(service.SequenceNumber, restorePoint.SequenceNumber);
            }
            catch (Win32Exception ex)
            {
                if (NativeMethods.ERROR_ACCESS_DENIED == ex.NativeErrorCode)
                {
                    Assert.Inconclusive("Elevation required.");
                }
                else
                {
                    throw;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Begins a system restore point.
        /// </summary>
        /// <param name="type">The type of restore point to create.</param>
        /// <param name="description">Optional description for the restore point. The default value is the assembly title.</param>
        /// <param name="service">The service provider to create or modify restore points. The default calls info the system service.</param>
        /// <returns>Information about the restore point or null if the service is disabled.</returns>
        /// <exception cref="Win32Exception">Failed to create the system restore point.</exception>
        internal static SystemRestorePoint Create(RestorePointType type, string description = null, ISystemRestoreService service = null)
        {
            if (RestorePointType.CancelledOperation == type)
            {
                throw new InvalidEnumArgumentException("type", (int)type, type.GetType());
            }

            if (string.IsNullOrEmpty(description))
            {
                // Get the assembly title metadata value.
                var title = (AssemblyTitleAttribute)Assembly.GetExecutingAssembly()
                            .GetCustomAttributes(typeof(AssemblyTitleAttribute), false)
                            .FirstOrDefault();

                if (null != title)
                {
                    description = title.Title;
                }
            }

            var info = new RestorePointInfo()
            {
                Description    = description,
                EventType      = RestorePointEventType.BeginSystemChange,
                SequenceNumber = 0,
                Type           = type,
            };

            var instance = new SystemRestorePoint()
            {
                _service = service ?? SystemRestorePoint.DefaultServiceProvider,
                _info    = info,
            };

            // Create the system restore point.
            var status = instance.SetRestorePoint(info);

            // Update the sequence number.
            info.SequenceNumber = status.SequenceNumber;
            instance._info      = info;

            return(instance);
        }
        public void FailRestorePointThrows()
        {
            var service = new SystemRestoreTestService();

            service.SetNextErrorCode(NativeMethods.ERROR_INVALID_DATA);

            try
            {
                SystemRestorePoint.Create(RestorePointType.ApplicationInstall, service: service);
            }
            catch (Win32Exception ex)
            {
                if (NativeMethods.ERROR_ACCESS_DENIED == ex.NativeErrorCode)
                {
                    Assert.Inconclusive("Elevation required.");
                }
                else
                {
                    throw;
                }
            }
        }
        public void CreateRestorePoint()
        {
            var service = new SystemRestoreTestService();

            try
            {
                var restorePoint = SystemRestorePoint.Create(RestorePointType.ApplicationInstall, "CreateRestorePoint test", service);
                restorePoint.Commit();

                Assert.AreEqual <string>("CreateRestorePoint test", restorePoint.Description);
                Assert.AreEqual <long>(service.SequenceNumber, restorePoint.SequenceNumber);
            }
            catch (Win32Exception ex)
            {
                if (NativeMethods.ERROR_ACCESS_DENIED == ex.NativeErrorCode)
                {
                    Assert.Inconclusive("Elevation required.");
                }
                else
                {
                    throw;
                }
            }
        }
        public void CancelRestorePointInvalid()
        {
            var restorePoint = SystemRestorePoint.Create(RestorePointType.CancelledOperation);

            Assert.Fail("Creating a system restore point to cancel an operation directly is invalid.");
        }