c# RegistryKey操作注册表
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Win32; namespace AirPatientForm { class RegistryDAO { public static string GetRegistData(string name) { if (!IsRegeditExit(name)) { CreateRegedit(@"d:\Airway\AirwayVE\Db\"); } string registData =""; RegistryKey hkml = Registry.CurrentUser; RegistryKey software = hkml.OpenSubKey("SOFTWARE"); RegistryKey aimdir = software.OpenSubKey("LungCare").OpenSubKey("AirwayNav"); registData = (string)aimdir.GetValue(name); return registData; } /// <summary> /// /// </summary> /// <param name="name">AirwayPatientsRootPath</param> /// <returns></returns> public static bool IsRegeditExit(string name) { bool _exit = false; string[] subkeyNames; RegistryKey hkml = Registry.CurrentUser; RegistryKey software = hkml.OpenSubKey("SOFTWARE", true); subkeyNames = software.GetSubKeyNames(); bool flag = false; foreach (string keyName in subkeyNames) { if (keyName == "LungCare") { flag = true; break; } } if (flag) { RegistryKey LungCare = software.OpenSubKey("LungCare", true); subkeyNames = LungCare.GetSubKeyNames(); bool flag1 = false; foreach (string keyName in subkeyNames) { if (keyName == "AirwayNav") { flag1 = true; break; } } if (flag1) { RegistryKey AirwayNav = LungCare.OpenSubKey("AirwayNav", true); subkeyNames = AirwayNav.GetValueNames(); bool flag2 = false; foreach (string keyName in subkeyNames) { if (keyName == name) { flag2 = true; _exit = true; return _exit; } } if (!flag2) { _exit = false; return _exit; } } else { _exit = false; } } else { _exit = false; } return _exit; } /// <summary> /// /// </summary> /// <param name="path">"d:\Airway\AirwayVE\Db\"</param> public static void CreateRegedit(string value) { RegistryKey hkml = Registry.CurrentUser; RegistryKey software = hkml.OpenSubKey("SOFTWARE", true); RegistryKey LungCare = software.CreateSubKey("LungCare"); RegistryKey AirwayNav = LungCare.CreateSubKey("AirwayNav"); AirwayNav.SetValue("AirwayPatientsRootPath", value, RegistryValueKind.String); } } }