2

私はC#にかなり慣れていません。複数のレジストリエントリを次々に削除したいのですが、最初のキーで例外が発生し、その後、コードは他のキーの削除を停止します.ifブロックの単一のキーで機能する以下のコードを試しました

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

    }
  }
4

2 に答える 2

0

最後に、私は多くの努力の後に機能しているより良いコードを持っています誰かがそれをより良くすることができるかどうか私に知らせてください

   namespace ConsoleApplication1
   {
    class Program
    {
    static void Main(string[] args)
    {
        string[] clsids = {
                          "{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[] keys = {  @"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"
};

        /*Classes root hive*/

        for (int i = 0; i<=7; i++ )
        {
        foreach (var regkey in keys)
        {
            try
            {

           using (RegistryKey mykey = Registry.ClassesRoot.OpenSubKey(regkey, true))
                    if (mykey != null)
                    {
                        Console.WriteLine(mykey + @"\"+clsids[i]);
                        mykey.DeleteSubKeyTree(clsids[i]);

                    }

                    else
                    {
                        Console.WriteLine("key does not exist");
                    }

                /*try end*/
            }
            catch
            {
                continue;
            }


        }
        }

        /*Current user hive*/

        for (int i = 0; i <= 7; i++)
        {
            foreach (var regkey in keys)
            {
                try
                {

             using (RegistryKey mykey = Registry.CurrentUser.OpenSubKey(regkey, true))
                        if (mykey != null)
                        {
                            Console.WriteLine(mykey + @"\" + clsids[i]);
                            mykey.DeleteSubKeyTree(clsids[i]);

                        }

                        else
                        {
                            Console.WriteLine("key does not exist");
                        }

                    /*try end*/
                }
                catch
                {
                    continue;
                }


            }
            }

        /*Local Machine hive*/

        for (int i = 0; i <= 7; i++)
        {
            foreach (var regkey in keys)
            {
                try
                {
            using (RegistryKey mykey = Registry.LocalMachine.OpenSubKey(regkey, true))
                        if (mykey != null)
                        {
                            Console.WriteLine(mykey + @"\" + clsids[i]);
                            mykey.DeleteSubKeyTree(clsids[i]);

                        }

                        else
                        {
                            Console.WriteLine("key does not exist");
                        }

                    /*try end*/
                }
                catch
                {
                    continue;
                }


            }
            }

        Console.WriteLine("Cleanup finished. Press any key to exit");
        Console.ReadLine();
    }
    }
    }
于 2013-02-13T14:32:23.113 に答える
0

あなたの詳細に与えられて、あなたは次のコードを試すことができます

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(...)存在しないサブキーを開いたり削除しようとし ます!おそらく、 Registry.OpenSubKeyDeleteSubKeyTree(..)を調べる必要があります。{55963676-2F5E-4BAF-AC28-CF26AA587566}

編集:与えられた詳細の時点で新しい答えを追加しました

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));
        }
    }
}
于 2013-02-10T17:53:32.897 に答える