Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7033771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:06:19+00:00 2026-05-28T01:06:19+00:00

I am sitting here finding myself writing a recursive call for C# to write

  • 0

I am sitting here finding myself writing a recursive call for C# to write a RegistryKey.

This is something I could hard code easily enough, but I’d to do it recursively.

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

private const string regKeyPath = @"Software\Apps\jp2code\net\TestApp";

static void Main() {
  string[] split = regKeyPath.Split('\\');
  RegistryKey key = null;
  try {
    keyMaker(Registry.LocalMachine, split);
  } finally {
    if (key != null) {
      key.Close();
    }
  }
  // continue on with Application.Run(new Form1());
}

So, keyMaker is what I want to be my recursive function.

private static void keyMaker(RegistryKey key, string[] path) {
  string subKey = null;
  string[] subKeyNames = key.GetSubKeyNames();
  foreach (var item in subKeyNames) {
    if (path[0] == item) {
      subKey = item;
    }
  }
  RegistryKey key2 = null;
  try {
    if (String.IsNullOrEmpty(subKey)) {
      key2 = key.CreateSubKey(subKey);
    } else {
      key2 = key.OpenSubKey(subKey);
    }
    keyMaker(key2, &path[1]); // <= NOTE! Not allowed/defined in C#
  } finally {
    key2.Close();
  }
}

So, I can’t simply pass the array starting with the next element of the array.

Is there a neat way to do this in C#?

The Registry bit has nothing to do with the problem but to add my real world problem to an array task.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:06:20+00:00Added an answer on May 28, 2026 at 1:06 am

    A simple way to be to change your method’s signature to include the starting index:

    void keyMaker(RegistryKey key, string[] path, int startIndex)
    

    Apart from that, you can use a LinkedList<T> or a Queue<T> instead of an array, and use LinkedList<T>.RemoveFirst() or Queue<T>.Dequeue() methods to remove their head elements.

    But you don’t need recursion to solve this at all (unless this is an exercise).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was sitting here wondering if it's possible to write a greasemonkey script that
Click here to see what I mean.. What could prevent these two from sitting
So I'm sitting here compiling XMonad for myself because I do not like being
I can't resolve this issue, I found a similar question here but: setting the
Here is my controller public function login() { $this->load->model('admin_model'); $access = $this->admin_model->check_login(); $data['content'] =
I'm sitting here with the following class: class TagCompound : public Tag { public:
I'm sitting here scratching my head here, I should have 2 rows of 4
I have this code to remove an existing meta tag to which i dont
Just wondering if anyone has encountered this and has a good fix. Here is
I'm using the TPL but I'm finding it tricky unit testing code that uses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.