【问题标题】:ways to check state of system restore in C#在 C# 中检查系统还原状态的方法
【发布时间】:2011-10-10 02:01:08
【问题描述】:

我正在检查系统还原状态检查(启用/禁用)。 研发后发现可以通过以下方式实现:

  1. srclient.dll 状态检查
  2. 编辑注册表键
  3. WMI-直到 XP 版本才可用..

1) 我需要有关如何从 C# 中的 SystemRestore Registry 检查注册表项值的帮助。

2) 如果我需要使用 C# 库中的可用功能设置或删除还原点,但我想在用户设置或删除还原点之前检查状态,我的程序代码可以正常工作。 如果有人帮忙找出解决方案,将不胜感激。

【问题讨论】:

    标签: c#


    【解决方案1】:

    这是我检查系统还原是否启用的方式:

    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");
    }
    

    使用 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);
    

    【讨论】:

    • 如何获取启用系统还原的磁盘驱动器?
    • @SepehrM 您找到问题的答案了吗,请告诉我,如果系统保护打开或关闭,我有同样的问题。
    【解决方案2】:

    这是我用于 VB.NET 的内容,它是 Paxamime 的(翻译的)代码的混搭,以及一些考虑到它正在运行的操作系统(位数)的其他代码,因此它可以获得正确的注册表项,没有错误:

    首先,操作系统检查代码:

        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
    

    然后,检查系统保护(又名系统还原)的状态:

       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
    

    然后你可以这样做:

    If JSE_IsSystemRestoreEnabled = True Then
      ' Do something
    End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      相关资源
      最近更新 更多