/// <summary> /// Starts system restore /// </summary> /// <param name="strDescription">The description of the restore</param> /// <param name="rt">The type of restore point</param> /// <param name="lSeqNum">Returns the sequence number</param> /// <returns>The status of call</returns> /// <seealso cref="Use EndRestore() or CancelRestore() to end the system restore"/> public static int StartRestore(string strDescription, RestoreType rt, out long lSeqNum) { RestorePointInfo rpInfo = new RestorePointInfo(); STATEMGRSTATUS rpStatus = new STATEMGRSTATUS(); if (!SysRestoreAvailable()) { lSeqNum = 0; return(-1); } try { // Prepare Restore Point rpInfo.dwEventType = BeginSystemChange; // By default we create a verification system rpInfo.dwRestorePtType = (int)rt; rpInfo.llSequenceNumber = 0; rpInfo.szDescription = strDescription; SRSetRestorePointW(ref rpInfo, out rpStatus); } catch (DllNotFoundException) { lSeqNum = 0; return(-1); } lSeqNum = rpStatus.llSequenceNumber; return(rpStatus.nStatus); }
/// <returns>The sequence number</returns> public static Int64 CreateRestorePoint(String description, SystemRestoreType type) { if (!IsSystemRestoreAvailable()) { throw new NotSupportedException("System Restore is not supported"); } if (description == null) { throw new ArgumentNullException("description"); } if (description.Length > 256) { throw new ArgumentException("description cannot exceed 256 characters"); } _isCreating = true; RestorePointInfo point = new RestorePointInfo(); StateManagerStatus status = new StateManagerStatus(); point.dwEventType = (Int32)SystemRestoreEventType.BeginSystemChange; point.dwRestorePtType = (Int32)type; point.llSequenceNumber = 0; point.szDescription = description; NativeMethods.SetSystemRestorePoint(ref point, out status); if (status.nStatus != 0) { // throw? } return(status.llSequenceNumber); }
/// <summary> /// Cancels restore call /// </summary> /// <param name="lSeqNum">The restore sequence number</param> /// <exception cref="System.ComponentModel.Win32Exception"> /// Thrown when STATEMGRSTATUS.nStatus doesn't equal 0 /// (ERROR_SUCCESS) /// </exception> internal static void CancelRestore(long lSeqNum) { var rpInfo = new RestorePointInfo(); var rpStatus = new STATEMGRSTATUS(); if (!SysRestoreAvailable()) { return; } try { rpInfo.dwEventType = EndSystemChange; rpInfo.dwRestorePtType = (int)RestoreType.CancelledOperation; rpInfo.llSequenceNumber = lSeqNum; SRSetRestorePointW(ref rpInfo, out rpStatus); } catch (DllNotFoundException) { rpStatus.nStatus = 2; } if (rpStatus.nStatus != 0) { throw new Win32Exception(rpStatus.nStatus); } }
public static int FirstRunRestore(string strDescription) { RestorePointInfo rpInfo = new RestorePointInfo(); STATEMGRSTATUS rpStatus = new STATEMGRSTATUS(); if (!SysRestoreAvailable()) { return(0); } try { // Prepare Restore Point rpInfo.dwEventType = BeginSystemChange; rpInfo.dwRestorePtType = (int)RestoreType.FirstRun; rpInfo.llSequenceNumber = 0; rpInfo.szDescription = strDescription; SRSetRestorePointW(ref rpInfo, out rpStatus); EndRestore(rpStatus.llSequenceNumber); } catch (DllNotFoundException) { return(0); } return(rpStatus.nStatus); }
/// <summary> /// Starts system restore /// Use SysRestore.EndRestore or SysRestore.CancelRestore to end the system restore /// </summary> /// <param name="description">The description of the restore</param> /// <param name="lSeqNum">Returns the sequence number</param> /// <exception cref="System.ComponentModel.Win32Exception"> /// Thrown when STATEMGRSTATUS.nStatus doesn't equal 0 /// (ERROR_SUCCESS) /// </exception> internal static void StartRestore(string description, out long lSeqNum) { var rpInfo = new RestorePointInfo(); var rpStatus = new STATEMGRSTATUS(); if (!SysRestoreAvailable()) { lSeqNum = 0; return; } try { // Prepare Restore Point rpInfo.dwEventType = BeginSystemChange; // By default we create a verification system rpInfo.dwRestorePtType = (int)RestoreType.Restore; rpInfo.llSequenceNumber = 0; rpInfo.szDescription = description; SRSetRestorePointW(ref rpInfo, out rpStatus); } catch (DllNotFoundException) { rpStatus.nStatus = 2; } if (rpStatus.nStatus != 0) { lSeqNum = 0; throw new Win32Exception(rpStatus.nStatus); } lSeqNum = rpStatus.llSequenceNumber; }
/// <summary> /// Cancels restore call /// </summary> /// <param name="lSeqNum">The restore sequence number</param> /// <returns>The status of call</returns> public static int CancelRestore(long lSeqNum) { RestorePointInfo rpInfo = new RestorePointInfo(); STATEMGRSTATUS rpStatus = new STATEMGRSTATUS(); if (!SysRestoreAvailable()) { return(-1); } try { rpInfo.dwEventType = EndSystemChange; rpInfo.dwRestorePtType = (int)RestoreType.CancelledOperation; rpInfo.llSequenceNumber = lSeqNum; SRSetRestorePointW(ref rpInfo, out rpStatus); } catch (DllNotFoundException) { return(-1); } return(rpStatus.nStatus); }
/// <summary> /// Creates or modifies the system restore point. /// </summary> /// <param name="info">Information about the restore point to create or modify.</param> /// <param name="status">Status information of the restore point created or modified.</param> /// <returns>True if the operation was successful; otherwise, false.</returns> /// <remarks> /// The error code is reset to 0 (success) after each call. /// </remarks> /// <seealso cref="SetNextErrorCode"/> public bool SetRestorePoint(RestorePointInfo info, out StateManagerStatus status) { status.SequenceNumber = this.SequenceNumber; status.ErrorCode = this.nextErrorCode; // Reset next error code. this.nextErrorCode = 0; return(0 == status.ErrorCode); }
/// <summary> /// Creates or modifies the system restore point. /// </summary> /// <param name="info">Information about the restore point to create or modify.</param> /// <param name="status">Status information of the restore point created or modified.</param> /// <returns>True if the operation was successful; otherwise, false.</returns> /// <remarks> /// The error code is reset to 0 (success) after each call. /// </remarks> /// <seealso cref="SetNextErrorCode"/> public bool SetRestorePoint(RestorePointInfo info, out StateManagerStatus status) { status.SequenceNumber = this.SequenceNumber; status.ErrorCode = this.nextErrorCode; // Reset next error code. this.nextErrorCode = 0; return 0 == status.ErrorCode; }
/// <summary> /// Starts system restore /// </summary> /// <param name="strDescription">The description of the restore</param> /// <param name="rt">The type of restore point</param> /// <param name="lSeqNum">Returns the sequence number</param> /// <param name="creationFrequency"> /// Under Win 8 or newer - Minimal amount of minutes since last restore point for this point to be created. /// 0 to always create, number for amount of minutes, -1 for default behaviour (24 hours). /// </param> /// <returns>The status of call</returns> /// <seealso> /// <cref>Use EndRestore() or CancelRestore() to end the system restore</cref> /// </seealso> public static int StartRestore(string strDescription, RestoreType rt, out long lSeqNum, int creationFrequency = -1) { var rpInfo = new RestorePointInfo(); STATEMGRSTATUS rpStatus; if (!SysRestoreAvailable()) { lSeqNum = 0; return(-1); } if (creationFrequency >= 0) { using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore", true)) { Debug.Assert(key != null, "SystemRestore key must exist after SysRestoreAvailable"); key.SetValue("SystemRestorePointCreationFrequency", creationFrequency, RegistryValueKind.DWord); } } try { // Prepare Restore Point rpInfo.dwEventType = BeginSystemChange; // By default we create a verification system rpInfo.dwRestorePtType = (int)rt; rpInfo.llSequenceNumber = 0; rpInfo.szDescription = strDescription; SRSetRestorePointW(ref rpInfo, out rpStatus); } catch (DllNotFoundException) { lSeqNum = 0; return(-1); } lSeqNum = rpStatus.llSequenceNumber; return(rpStatus.nStatus); }
public static void CancelRestorePoint(Int64 sequenceNumber) { if (!IsSystemRestoreAvailable()) { throw new NotSupportedException("System Restore is not supported"); } RestorePointInfo point = new RestorePointInfo(); StateManagerStatus status = new StateManagerStatus(); point.dwEventType = (Int32)SystemRestoreEventType.EndSystemChange; point.dwRestorePtType = (Int32)SystemRestoreType.ApplicationCancelled; point.llSequenceNumber = sequenceNumber; NativeMethods.SetSystemRestorePoint(ref point, out status); if (status.nStatus != 0) { // throw? } }
/// <summary> /// Cancels restore call /// </summary> /// <param name="lSeqNum">The restore sequence number</param> /// <returns>The status of call</returns> internal static int CancelRestore( long lSeqNum ) { RestorePointInfo rpInfo = new RestorePointInfo(); STATEMGRSTATUS rpStatus = new STATEMGRSTATUS(); if ( !SysRestoreAvailable() ) return -1; try { rpInfo.dwEventType = EndSystemChange; rpInfo.dwRestorePtType = (int)RestoreType.CancelledOperation; rpInfo.llSequenceNumber = lSeqNum; SRSetRestorePointW( ref rpInfo, out rpStatus ); } catch ( DllNotFoundException ) { return -1; } return rpStatus.nStatus; }
/// <summary> /// Ends system restore call /// </summary> /// <param name="lSeqNum">The restore sequence number</param> /// <returns>The status of call</returns> public static int EndRestore(long lSeqNum) { var rpInfo = new RestorePointInfo(); STATEMGRSTATUS rpStatus; if (!SysRestoreAvailable()) { return(-1); } try { rpInfo.dwEventType = EndSystemChange; rpInfo.llSequenceNumber = lSeqNum; SRSetRestorePointW(ref rpInfo, out rpStatus); } catch (DllNotFoundException) { return(-1); } return(rpStatus.nStatus); }
/// <summary> /// Starts system restore /// </summary> /// <param name="strDescription">The description of the restore</param> /// <param name="rt">The type of restore point</param> /// <param name="lSeqNum">Returns the sequence number</param> /// <returns>The status of call</returns> /// <seealso cref="Use EndRestore() or CancelRestore() to end the system restore"/> internal static int StartRestore( string strDescription, RestoreType rt, out long lSeqNum ) { RestorePointInfo rpInfo = new RestorePointInfo(); STATEMGRSTATUS rpStatus = new STATEMGRSTATUS(); if ( !SysRestoreAvailable() ) { lSeqNum = 0; return -1; } try { // Prepare Restore Point rpInfo.dwEventType = BeginSystemChange; // By default we create a verification system rpInfo.dwRestorePtType = (int)rt; rpInfo.llSequenceNumber = 0; rpInfo.szDescription = strDescription; SRSetRestorePointW( ref rpInfo, out rpStatus ); } catch ( DllNotFoundException ) { lSeqNum = 0; return -1; } lSeqNum = rpStatus.llSequenceNumber; return rpStatus.nStatus; }
internal static extern bool SRSetRestorePointW( ref RestorePointInfo pRestorePtSpec, out STATEMGRSTATUS pSMgrStatus );
public static int FirstRunRestore(string strDescription) { RestorePointInfo rpInfo = new RestorePointInfo(); STATEMGRSTATUS rpStatus = new STATEMGRSTATUS(); if (!SysRestoreAvailable()) return 0; try { // Prepare Restore Point rpInfo.dwEventType = BeginSystemChange; rpInfo.dwRestorePtType = (int)RestoreType.FirstRun; rpInfo.llSequenceNumber = 0; rpInfo.szDescription = strDescription; SRSetRestorePointW(ref rpInfo, out rpStatus); EndRestore(rpStatus.llSequenceNumber); } catch (DllNotFoundException) { return 0; } return rpStatus.nStatus; }
/// <summary> /// Ends system restore call /// </summary> /// <param name="lSeqNum">The restore sequence number</param> /// <returns>The status of call</returns> public static int EndRestore(long lSeqNum) { RestorePointInfo rpInfo = new RestorePointInfo(); STATEMGRSTATUS rpStatus = new STATEMGRSTATUS(); if (!SysRestoreAvailable()) return 0; try { rpInfo.dwEventType = EndSystemChange; rpInfo.llSequenceNumber = lSeqNum; SRSetRestorePointW(ref rpInfo, out rpStatus); } catch (DllNotFoundException) { return 0; } return rpStatus.nStatus; }
/// <summary> /// Starts system restore /// </summary> /// <param name="strDescription">The description of the restore</param> /// <param name="lSeqNum">Returns the sequence number</param> /// <returns>The status of call</returns> public static int StartRestore(string strDescription, out long lSeqNum) { if (Environment.OSVersion.Version.Major > 6 || Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 2) { // We have Windows 8 and therefore using WMI. try { ManagementScope oScope = new ManagementScope("\\\\localhost\\root\\default"); ManagementPath oPath = new ManagementPath("SystemRestore"); ObjectGetOptions oGetOp = new ObjectGetOptions(); ManagementClass oProcess = new ManagementClass(oScope, oPath, oGetOp); ManagementBaseObject oInParams = oProcess.GetMethodParameters("CreateRestorePoint"); oInParams["Description"] = strDescription; oInParams["RestorePointType"] = 12; // MODIFY_SETTINGS oInParams["EventType"] = 100; ManagementBaseObject oOutParams = oProcess.InvokeMethod("CreateRestorePoint", oInParams, null); } catch (Exception) { // ToDo: send exception details via SmartAssembly bug reporting! } lSeqNum = 0; return 0; } else { var rpInfo = new RestorePointInfo(); STATEMGRSTATUS rpStatus; if (!SysRestoreAvailable()) { lSeqNum = 0; return 0; } try { // Prepare Restore Point rpInfo.dwEventType = BeginSystemChange; // By default we create a verification system rpInfo.dwRestorePtType = (int)RestoreType.Restore; rpInfo.llSequenceNumber = 0; rpInfo.szDescription = strDescription; SRSetRestorePointW(ref rpInfo, out rpStatus); lSeqNum = rpStatus.llSequenceNumber; return rpStatus.nStatus; } catch (DllNotFoundException) { } catch (Exception) { // ToDo: send exception details via SmartAssembly bug reporting! } lSeqNum = 0; return 0; } }
/// <summary> /// Ends system restore call /// </summary> /// <param name="lSeqNum">The restore sequence number</param> /// <returns>The status of call</returns> public static int EndRestore(long lSeqNum) { if (Environment.OSVersion.Version.Major > 6 || Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 2) { // There is nothing to do for Windows 8 here. return 0; } else { var rpInfo = new RestorePointInfo(); STATEMGRSTATUS rpStatus; if (!SysRestoreAvailable()) return 0; try { rpInfo.dwEventType = EndSystemChange; rpInfo.llSequenceNumber = lSeqNum; SRSetRestorePointW(ref rpInfo, out rpStatus); } catch (DllNotFoundException) { return 0; } catch (Exception) { // ToDo: send exception details via SmartAssembly bug reporting! return 0; } return rpStatus.nStatus; } }
public RestorePoint(string description) { this.description = description; STATEMGRSTATUS status = new STATEMGRSTATUS(); RestorePointInfo rpi = new RestorePointInfo(); rpi.dwEventType = BeginSystemChange; rpi.dwRestorePtType = (int)RestoreType.Checkpoint; rpi.llSequenceNumber = 0; rpi.szDescription = description; try { this.set = SRSetRestorePointW(ref rpi, out status); } catch { this.set = false; Trace.WriteLine("System Restore Point Not Set (System restore points don't exist on sever class versions of Windows)"); } this.restoreseq = status.llSequenceNumber; }
public void End() { if (this.set) { STATEMGRSTATUS status = new STATEMGRSTATUS(); RestorePointInfo rpi = new RestorePointInfo(); rpi.dwEventType = EndSystemChange; rpi.dwRestorePtType = (int)RestoreType.Checkpoint; rpi.llSequenceNumber = this.restoreseq; rpi.szDescription = description; this.set = SRSetRestorePointW(ref rpi, out status); } }
internal static extern bool SRSetRestorePointW(ref RestorePointInfo pRestorePtSpec, out STATEMGRSTATUS pSMgrStatus);
public RestorePoint(string description) { this.description = description; STATEMGRSTATUS status = new STATEMGRSTATUS(); RestorePointInfo rpi = new RestorePointInfo(); rpi.dwEventType = BeginSystemChange; rpi.dwRestorePtType = (int)RestoreType.Checkpoint; rpi.llSequenceNumber = 0; rpi.szDescription = description; this.set = SRSetRestorePointW(ref rpi, out status); this.restoreseq = status.llSequenceNumber; }