تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
كيفية عمل Restore Point مع جميع خصائصه باستعمال دوال ال API
#1
كاتب الموضوع : Boutemine Oualid

بسم الله الرحمن الرحيم
السلام عليكم و رحمة الله و بركاته
في هذه الشفرة نتعلم كيفية بناء نقطة استعادة نظام وذلك بدوال ال API


كود :
using System;
using System.Runtime.InteropServices;
namespace CreateRestorePointCs
{
/// <summary>
/// System Restore Point
/// use the RestoreAvailable to verify if restore operation is possible on the running system
/// and then call the function StartRestore before adding restore points
/// , and at the end the EndRestore function.
/// </summary>
public class RestorePoint
{
private Int64 rpSequenceNumber;
#region Public Constants
// Constantes
public const Int16 BeginSystemChange = 100; //Debut d'operation
public const Int16 EndSystemChange= 101; // fin d'operation
// for Windows XP only- used to prevent nested restore points.
public const Int16 BeginNestedSystemChange= 102;
public const Int16 EndNestedSystemChange= 103;
//---------------------------------------------
#endregion
#region Public Restore Type Enum
// Restore point types.
public enum RestoreType
{
ApplicationInstall = 0, // New application installation
ApplicationUninstall = 1, // Existing Application uninstallation
ModifySettings = 12, // System modification
CancelledOperation = 13, // END_SYSTEM_CHANGE
Restore = 6, // System restore
Checkpoint = 7,
DeviceDriverInstall = 10, // Driver installation
FirstRun = 11, // First application run
BackupRecovery = 14
}
#endregion
#region Methods
/// <summary>
/// Verify if the OS supports the restore points opperation.
/// </summary>
/// <returns>Result.</returns>
public bool RestoreAvailable()
{
int majorVersion = Environment.OSVersion.Version.Major;
int minorVersion = Environment.OSVersion.Version.Minor;
switch (Environment.OSVersion.Platform)
{
case PlatformID.Win32Windows:
// Windows Me
if (minorVersion == 90)
{
return true;
}
else
{
return false;
}
case PlatformID.Win32NT:
switch (majorVersion)
{
case 5:
if (minorVersion == 0)
{
return false;
}
// Windows XP
else if (minorVersion == 1)
{
return true;
}
// Windows Server 2003
else if (minorVersion == 2)
{
return false;
}
else
{
return false;
}
case 6:
// Windows Vista
if (minorVersion == 0)
{
return true;
}
// Windows Server 2008
else if (minorVersion == 1)
{
return false;
}
else
{
return false;
}
default:
return false;
}
default:
return false;
}
}
/// <summary>
/// Modification process beginning
/// </summary>
public bool StartRestore(string description, RestoreType lType)
{
// Restore point informations.
NativeMethods.RestorePointInfo rpInfo;
// Restore point state.
NativeMethods.SystemManagerRestoreStatus rpStatus;
//If the last operation isn't finished
if (rpSequenceNumber != 0)
{
System.Windows.Forms.MessageBox.Show("You must terminate the running restoration process.",
System.Windows.Forms.Application.ProductName, System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Asterisk);
return false;
}

rpInfo.dwEventType = BeginSystemChange;
rpInfo.dwRestorePtType = (int) lType;
rpInfo.llSequenceNumber = 0; // operation starts
rpInfo.szDescription = description;
// if the process successed.
if (NativeMethods.SRSetRestorePointW(ref rpInfo, out rpStatus))
{
// We take the new index of the restore point.
rpSequenceNumber = rpStatus.llSequenceNumber;
return true;
}
else
{
return false;
}
}
/// <summary>
/// Overriden version
/// </summary>
public bool StartRestore(string description)
{
NativeMethods.RestorePointInfo rpInfo;
NativeMethods.SystemManagerRestoreStatus rpStatus;
if (rpSequenceNumber != 0)
{
System.Windows.Forms.MessageBox.Show("Tu dois d'abord terminer le point de restauration.",
System.Windows.Forms.Application.ProductName, System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Asterisk);
return false;
}
rpInfo.dwEventType = BeginSystemChange;
rpInfo.dwRestorePtType = (int) RestoreType.Checkpoint;
rpInfo.llSequenceNumber = 0;
rpInfo.szDescription = description;
if (NativeMethods.SRSetRestorePointW(ref rpInfo, out rpStatus))
{
rpSequenceNumber = rpStatus.llSequenceNumber;
return true;
}
else
{
return false;
}
}
/// <summary>
/// system restore end.
/// </summary>
public bool EndRestore()
{
NativeMethods.RestorePointInfo rpInfo;
NativeMethods.SystemManagerRestoreStatus rpStatus;
rpInfo.dwEventType = EndSystemChange;
rpInfo.llSequenceNumber = rpSequenceNumber;
rpInfo.dwRestorePtType = 0;
rpInfo.szDescription = "";
rpSequenceNumber = 0;
return NativeMethods.SRSetRestorePointW(ref rpInfo, out rpStatus);
}
#endregion
}
#region Native Methods
internal class NativeMethods
{
// private constructor
private NativeMethods() { }
internal const Int16 DesktopSetting = 2;
internal const Int16 AccessibilitySetting = 3;
internal const Int16 OeSetting = 4;
internal const Int16 ApplicationRun = 5;
internal const Int16 WindowsShutdown = 8;
internal const Int16 WindowsBoot = 9;
internal const Int16 MaxDesc = 64;
internal const Int16 MaxDescW = 256;

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
internal struct RestorePointInfo
{
internal int dwEventType; // Event type
internal int dwRestorePtType; // Restore point type
internal Int64 llSequenceNumber;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MaxDescW + 1)]
internal string szDescription;
}
[StructLayout(LayoutKind.Sequential)]
internal struct SystemManagerRestoreStatus
{
internal int nStatus;
internal Int64 llSequenceNumber;
}

[DllImport("srclient.dll")]
[return : MarshalAs(UnmanagedType.Bool)]
internal static extern bool SRSetRestorePointW(ref RestorePointInfo pRestorePtSpec, out SystemManagerRestoreStatus pSMgrStatus);
[DllImport("srclient.dll")]
internal static extern int SRRemoveRestorePoint(int dwRPNum);
}
#endregion
}
القيام بهذه العملية يتطلب صلاحيات مدير لذلك يجب اضافة ملف Manifest للمشروع


كود :
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</asmv1:assembly>
}}}
تم الشكر بواسطة:


المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  استخدام دوال مكتبة من ملف DLL بدون إضافته كمرجع Reference @@أبورائد@@ 7 7,832 18-11-21, 05:30 AM
آخر رد: kaled2025
  شرح كيفية برمجة( مثال نسخ الملفات من مكان انت تحدده الى مكان انت تحدده بامتداد انت تحدده) سعود 5 8,081 04-09-19, 04:32 AM
آخر رد: سعود
  شرح كيفية صنع محرر html واستخدامه كـ dll سعود 6 5,981 26-08-13, 09:21 PM
آخر رد: Sajad
  كيفية عمل صورة - مدورة - Rouded PictureBox ! RaggiTech 0 2,888 17-10-12, 09:55 PM
آخر رد: RaggiTech
  كيفية عمل String عشوائي ! RaggiTech 0 2,682 17-10-12, 09:54 PM
آخر رد: RaggiTech
  فائدة - كيفية عمل Ping RaggiTech 0 3,548 17-10-12, 09:00 PM
آخر رد: RaggiTech
  كيفية تحويل DataTable إلى Object List RaggiTech 0 2,503 17-10-12, 07:27 PM
آخر رد: RaggiTech
  كيفية ادارة صورة 360 درجة RaggiTech 0 2,199 17-10-12, 07:24 PM
آخر رد: RaggiTech
  للمبتدئين كيفية صنع فورم على شكل قلب او دائرة او متقاطعات او ما شابه مثال مع شرح مبسط RaggiTech 0 2,527 17-10-12, 07:15 PM
آخر رد: RaggiTech
  للمبتدئين فقط..كيفية عمل برنامج صغير (عداد لحساب الذاكرة المستخدمة بالجهاز) RaggiTech 0 2,351 17-10-12, 07:13 PM
آخر رد: RaggiTech

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم