Рекурсивные вызовы и массивы C #

Я сижу здесь и обнаруживаю, что пишу рекурсивный вызов C # для записи RegistryKey .

Это то, что я мог бы достаточно легко закодировать, но я бы сделал это рекурсивно.

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

Итак, keyMaker - это моя рекурсивная функция.

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

Итак, я не могу просто передать массив, начиная со следующего элемента массива.

Есть ли удобный способ сделать это в C #?

Бит Registry не имеет ничего общего с проблемой, но добавляет мою реальную проблему в задачу массива.

0
задан jp2code 11 January 2012 в 22:41
поделиться