c# - 是否可以一个接一个地删除多个注册表项?

标签 c# registry registrykey

我是 c# 的新手。我想一个接一个地删除多个注册表项,但我第一个键出现异常,之后代码停止删除其他键。我在下面尝试过提到的代码它适用于 if block 中的单个键

string[] keyArray = { @"Wow6432Node\CLSID",
            @"Software\Microsoft\Windows\CurrentVersion\Ext\Settings",
            @"Software\Microsoft\Windows\CurrentVersion\Ext\Stats",
            @"SOFTWARE\Classes\Wow6432Node\CLSID",
            @"SOFTWARE\Wow6432Node\Classes\CLSID",
            @"Wow6432Node\Microsoft\Code Store Database\Distribution Units",
            @"Wow6432Node\Microsoft\Internet Explorer\ActiveX Compatibility",
            @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\ModuleUsage"
    };

foreach (var key in keyArray)
{
    try
    {

        RegistryKey myKey = Registry.ClassesRoot.OpenSubKey(key, true);
        if (myKey != null)
        {
            /*Cisco Secure Desktop*/
            myKey.DeleteSubKeyTree("{705EC6D4-B138-4079-A307-EF13E4889A82}");
            myKey.DeleteSubKeyTree("{F8FC1530-0608-11DF-2008-0800200C9A66}");
            myKey.DeleteSubKeyTree("{E34F52FE-7769-46CE-8F8B-5E8ABAD2E9FC}");
            /*Cisco Hostscan*/
            myKey.DeleteSubKeyTree("{F8FC1530-0608-11DF-2008-0800200C9A66}");
            myKey.DeleteSubKeyTree("{E34F52FE-7769-46CE-8F8B-5E8ABAD2E9FC}");
            /*Cisco AnyConnect Secure Mobility Client*/
            myKey.DeleteSubKeyTree("{55963676-2F5E-4BAF-AC28-CF26AA587566}");
            myKey.DeleteSubKeyTree("{CC679CB8-DC4B-458B-B817-D447B3B6AC31}");
        }
        else
            Console.WriteLine(string.Format("could not open key: {0}", key));
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(
            string.Format("action {0} failed with\n{1}", key, ex.Message));

    }
  }

最佳答案

鉴于您的详细信息,您可以尝试以下代码

string[] keyArray = { @"Wow6432Node\CLSID",
                @"Software\Microsoft\Windows\CurrentVersion\Ext\Settings",
                @"Software\Microsoft\Windows\CurrentVersion\Ext\Stats",
                @"Wow6432Node\Microsoft\Code Store Database\Distribution Units",
                @"Wow6432Node\Microsoft\Internet Explorer\ActiveX Compatibility",
                @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\ModuleUsage",
                @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\ModuleUsage",
        };

foreach (var key in keyArray)
{   
    try
    {
        RegistryKey myKey = Registry.ClassesRoot.OpenSubKey(key, true);
        if (myKey != null)
            myKey.DeleteSubKeyTree("{55963676-2F5E-4BAF-AC28-CF26AA587566}");
        else
            System.Diagnostics.Debug.WriteLine(string.Format("could not open key: {0}", key));
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(
            string.Format("action {0} failed with\n{1}", key, ex.Message));
    }                
}

这也应该打印出一些错误信息。

根据您给出的评论 ArgumentException(...),您尝试打开/删除一个子项 {55963676-2F5E-4BAF-AC28-CF26AA587566} 那不存在!也许你应该看看 Registry.OpenSubKeyDeleteSubKeyTree(..)

编辑:根据给定的详细信息添加了一个新答案

private static void deleteRegistryKeys2()
{
    string[] classIds = {
                    "{538793D5-659C-4639-A56C-A179AD87ED44}",
                        /*Cisco Secure Desktop*/
                    "{705EC6D4-B138-4079-A307-EF13E4889A82}",
                    "{F8FC1530-0608-11DF-2008-0800200C9A66}",
                        /*Cisco Hostscan*/
                    "{F8FC1530-0608-11DF-2008-0800200C9A66}",
                    "{E34F52FE-7769-46CE-8F8B-5E8ABAD2E9FC}",
                        /*Cisco AnyConnect Secure Mobility Client*/
                    "{55963676-2F5E-4BAF-AC28-CF26AA587566}",
                    "{CC679CB8-DC4B-458B-B817-D447B3B6AC31}"
                    };

    string[] regKeys = {  @"Wow6432Node\CLSID",
                        @"Software\Microsoft\Windows\CurrentVersion\Ext\Settings",
                        @"Software\Microsoft\Windows\CurrentVersion\Ext\Stats",
                        @"SOFTWARE\Classes\Wow6432Node\CLSID",
                        @"SOFTWARE\Wow6432Node\Classes\CLSID",
                        @"Wow6432Node\Microsoft\Code Store Database\Distribution Units",
                        @"Wow6432Node\Microsoft\Internet Explorer\ActiveX Compatibility",
                        @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\ModuleUsage"
                    };

    // iterate every regKey
    foreach (var regkey in regKeys)
    {
        // go for each classId
        foreach (var classId in classIds)
        {
            // get a regKey within a hive, pass classId too
            deleteKey(Registry.ClassesRoot.OpenSubKey(regkey, true), classId);

            deleteKey(Registry.CurrentUser.OpenSubKey(regkey, true), classId);
            deleteKey(Registry.LocalMachine.OpenSubKey(regkey, true), classId);
        }
    }
    Console.WriteLine("Cleanup finished. Press any key to exit");
    Console.ReadLine();
}

private static void deleteKey(RegistryKey registryKey, string classId)
{
    // check if there is a key
    if (registryKey != null)
    {
        try
        {
            // try to remove classId within this regKey
            registryKey.DeleteSubKeyTree(classId);
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
            Console.WriteLine(
                string.Format("could not delete key {0} with classId {1}",
                    registryKey, classId));
        }
    }
}

关于c# - 是否可以一个接一个地删除多个注册表项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14799754/

相关文章:

c# - 无法从现有键中读取值

iis - MaxWebConfigFileSizeInKB 和 IIS 10

c# - 将函数包装为异步任务

c# - 如何使用 MS 图表控件将 y 轴值增加 15 分钟

php - 如何在 PHP 桌面/GTK 应用程序中访问系统注册表?

java - 在Java或C#中正确获取操作系统信息

c# - 单声道 winforms 中的版权和商标符号

javascript - 从前端发送文件到后端花费的时间太长

windows - 在没有 MultipleInvokePromptMinimum 的情况下通过上下文菜单打开超过 15 个文件

c# - 通过 C# 在 Windows 注册表中创建一个 REG_NONE 空值