RegistryKey k = Registry.LocalMachine.OpenSubKey("SoftWare//Microsoft//sxLdfang"); // 打开用于“读” if (k == null) // 无此子项 { k = Registry.LocalMachine.OpenSubKey("SoftWare//Microsoft", true).CreateSubKey("sxLdfang"); // true表示该项有写的权限 k.SetValue("字符串值", "Hello,中国!"); // 创建一“字符串值” k.Close(); } else { if (k.ValueCount < 5) // 值的个数不包含“默认”值 { k.Close(); k = Registry.LocalMachine.OpenSubKey("SoftWare//Microsoft//sxLdfang", true); // 以“写”权限重新打开 k.SetValue("二进制值", new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); // 写入“字节数组” k.SetValue("DWORD值", 0x69ABCDEF); // 写入“整数” k.SetValue("多字符串值", new string[] { "张三丰", "李四光", "杨百万" }); // 写入“字符串数组” k.SetValue("可扩充字符串", "系统根目录:%SystemRoot%", RegistryValueKind.ExpandString); // 写入“可扩充字符串” k.Close(); } else { string s = k.GetValue("字符串值").ToString(); int i = (int)k.GetValue("DWORD值"); string[] sm = (string[])k.GetValue("多字符串值"); byte[] b = (byte[])k.GetValue("二进制值"); string sk = k.GetValue("可扩充字符串").ToString(); // 系统根目录:C:/WINDOWS this.Text = sk; k.Close(); } }