打印 LocalMachine 的整个树内容

发布于 2024-12-10 18:27:44 字数 1584 浏览 0 评论 0原文

我正在尝试开发 ac# 程序以将 LocalMachine 的整个树打印到控制台。到目前为止,我只能获取 HKEY_LOCAL_MACHINE 的子项,但没有比这更深入的了。我相对确定我需要在这里使用某种递归来获取子项及其子项的所有内容,等等。我只是不知道该怎么做。这是我现在得到的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace PrintLocalMachine
{
    class PrintLocalMachine
    {
        static void Main(string[] args)
        {
            Console.Out.WriteLine(Registry.LocalMachine.Name);
            string[] subkeynames = Registry.LocalMachine.GetSubKeyNames();            

            foreach (string subkey in subkeynames)
            {
                try
                {
                    RegistryKey rk = Registry.LocalMachine.OpenSubKey(subkey);
                    Console.Out.WriteLine(rk.Name);

                    string[] subkeynames2 = rk.GetSubKeyNames();

                    foreach (string s in subkeynames2)
                    {
                        recurse(s, rk);
                    }
                }
                catch (Exception e) { }
            }
        }

        private static void recurse(string sub, RegistryKey rk)
        {
            RegistryKey rk2 = Registry.LocalMachine.OpenSubKey(sub);
            Console.Out.WriteLine(rk2.Name);

            string[] subkeynames3 = rk.GetSubKeyNames();

            foreach(string s2 in subkeynames3){
                recurse(s2, rk2);
            }
        }
    }
}

有人可以解释我应该如何解决这个问题吗?我真的只需要指明正确的方向,我在这方面碰壁了。

编辑:我做了一些更改并更新了代码;更新后的代码挂在 HKEY_CURRENT_MACHINE\SAM 上,只是一遍又一遍地打印它,直到 StackOverflowException

I'm trying to develop a c# program to print the entire tree of LocalMachine to the console. So far I've just been able to get the subkeys of HKEY_LOCAL_MACHINE, but nothing deeper than that. I'm relatively sure I need to use some kind of recursion here to get all the contents of subkeys and their subkeys, and so on. I'm just not sure how to go about it. Here's what I've got as of now:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace PrintLocalMachine
{
    class PrintLocalMachine
    {
        static void Main(string[] args)
        {
            Console.Out.WriteLine(Registry.LocalMachine.Name);
            string[] subkeynames = Registry.LocalMachine.GetSubKeyNames();            

            foreach (string subkey in subkeynames)
            {
                try
                {
                    RegistryKey rk = Registry.LocalMachine.OpenSubKey(subkey);
                    Console.Out.WriteLine(rk.Name);

                    string[] subkeynames2 = rk.GetSubKeyNames();

                    foreach (string s in subkeynames2)
                    {
                        recurse(s, rk);
                    }
                }
                catch (Exception e) { }
            }
        }

        private static void recurse(string sub, RegistryKey rk)
        {
            RegistryKey rk2 = Registry.LocalMachine.OpenSubKey(sub);
            Console.Out.WriteLine(rk2.Name);

            string[] subkeynames3 = rk.GetSubKeyNames();

            foreach(string s2 in subkeynames3){
                recurse(s2, rk2);
            }
        }
    }
}

Could someone explain how I should go about this? I really just need to be pointed in the right direction, I've just hit a wall with this.

EDIT: I changed a bit and updated the code; the updated code is hanging on HKEY_CURRENT_MACHINE\SAM, just printing it over and over until StackOverflowException

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏の忆 2024-12-17 18:27:44

recurse() 不是递归,除非它调用自身。
将所有代码从 main 移至 recurse() 并从 main 调用 recurse() 。
您可能还想关闭打开的子项。

recurse() is not recursion unless it calls itself.
Move all your code from main to recurse() and call recurse() from main.
You might also want to close opened subkeys.

把昨日还给我 2024-12-17 18:27:44

你是对的。这种结构内有结构的问题可以通过递归来解决。您需要做的是编写一个递归函数,即一个调用自身直到满足某个条件的函数。在这种情况下,条件是,如果注册表项至少有一个子项,我们需要进入该注册表项,并且我们将继续这样做,直到到达叶节点,即没有更多子项的注册表项。

 private static void Main(string[] args)
    {

        string[] subkeynames = Registry.LocalMachine.GetSubKeyNames();
        Console.Out.WriteLine(Registry.LocalMachine.Name);

        foreach (string subkey in subkeynames)
        {
            try
            {
                //this might raise a security exception
                RegistryKey rk = Registry.LocalMachine.OpenSubKey(subkey);
                recurse(rk);
                rk.Close();
            }
            catch (Exception e)
            {
                Console.Write("Couldnt access key : " + subkey + "\n " + e.ToString());
            }
        }

        Console.ReadKey();
    }

    private static void recurse(RegistryKey rk)
    {
        Console.WriteLine(rk.Name);
        string[] subkeys = rk.GetSubKeyNames();

        if (null != subkeys && subkeys.Count() > 0)
        {
            foreach (var subkey in subkeys)
            {
                try
                {
                    //this might raise a security exception
                    RegistryKey key = rk.OpenSubKey(subkey);
                    recurse(key);
                }
                catch (Exception e)
                {
                    Console.Write("Couldnt access key : " + subkey + "\n " + e.ToString());
                }

            }
        }
    }

You are right. This kind of problem where you have a structure inside a structure can be solved through recursion. What you need to do is write a recursive function i.e. a function that calls itself until a certain condition satisfies. In this case that condition would be that if a Registry Key has at least one child we need to go inside that Registry Key and we will continue to do so until we reach a leaf node i.e. a Registry Key which has no more children.

 private static void Main(string[] args)
    {

        string[] subkeynames = Registry.LocalMachine.GetSubKeyNames();
        Console.Out.WriteLine(Registry.LocalMachine.Name);

        foreach (string subkey in subkeynames)
        {
            try
            {
                //this might raise a security exception
                RegistryKey rk = Registry.LocalMachine.OpenSubKey(subkey);
                recurse(rk);
                rk.Close();
            }
            catch (Exception e)
            {
                Console.Write("Couldnt access key : " + subkey + "\n " + e.ToString());
            }
        }

        Console.ReadKey();
    }

    private static void recurse(RegistryKey rk)
    {
        Console.WriteLine(rk.Name);
        string[] subkeys = rk.GetSubKeyNames();

        if (null != subkeys && subkeys.Count() > 0)
        {
            foreach (var subkey in subkeys)
            {
                try
                {
                    //this might raise a security exception
                    RegistryKey key = rk.OpenSubKey(subkey);
                    recurse(key);
                }
                catch (Exception e)
                {
                    Console.Write("Couldnt access key : " + subkey + "\n " + e.ToString());
                }

            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文