开发者

ways to check state of system restore in C#

I am working to check the system restore status check(enabled/disabled). After R&D I found out that it can be done in following ways:

  1. status check on srclient.dll
  2. editing Registery Keys
  3. WMI-available till XP edition..

1) I need help on how to check registry key value from SystemRestore Registry in C#.

2) My program code works fine if i need to set or remove restore point with the available functionality in C # libraries but i want to check the status before user sets or removes the restore point. would appreciate if开发者_JS百科 anyone helps out to find out a solution to this.


This is how I check to see if System Restore is enabled:

RegistryKey rk = Registry.LocalMachine;
RegistryKey rk1 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore");
string sysRestore = rk1.GetValue("RPSessionInterval").ToString();
if (sysRestore.Contains("1"))
{
    MessageBox.Show("System Restore is Enabled");
}

if (sysRestore.Contains("0"))
{
    MessageBox.Show("System Restore is Disabled");
}

To enable System Restore with WMI:

string osDrive = Path.GetPathRoot(Environment.SystemDirectory);

ManagementScope scope = new ManagementScope("\\\\localhost\\root\\default");
ManagementPath path = new ManagementPath("SystemRestore");
ObjectGetOptions options = new ObjectGetOptions();
ManagementClass process = new ManagementClass(scope, path, options);
ManagementBaseObject inParams = process.GetMethodParameters("Enable");
inParams["WaitTillEnabled"] = true;
inParams["Drive"] = osDrive;
ManagementBaseObject outParams = process.InvokeMethod("Enable", inParams, null);


Here's what I'm using for VB.NET, which is a mish-mash of Paxamime's (translated) code, and some other code that takes into consideration what OS (bitness) it's being run on, so it can get the right registry key, without error:

First, the OS checking code:

    Private Function JSE_ReadRegistry() As RegistryKey

        ' Read the SubKey names from the registry
        Dim rkKey As RegistryKey = Nothing

        If Environment.Is64BitOperatingSystem Then
            rkKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
        Else
            rkKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)
        End If

        rkKey.Close()

        Return rkKey

    End Function

Then, the checking of System Protection's (a.k.a. System Restore) state:

   Public Function JSE_IsSystemRestoreEnabled() As Boolean

        Dim rk As RegistryKey = JSE_ReadRegistry()
        Dim rk1 As RegistryKey = rk.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore")
        Dim strRestore As String = rk1.GetValue("RPSessionInterval").ToString()

        If strRestore.Contains("1") Then

            Debug.Print("System Restore is Enabled")
            Return True

        ElseIf strRestore.Contains("0") Then

            Debug.Print("System Restore is Disabled")
            Return False

        Else

            Debug.Print("IsSystemRestoreEnabled(): No Idea, JACK!")
            Return False

        End If

    End Function

Then you can just do something like:

If JSE_IsSystemRestoreEnabled = True Then
  ' Do something
End If
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜