//1.向注册表中写信息 using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"", true)) { if (key == null) { using (RegistryKey myKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\EricSun\MyTestKey")) { myKey.SetValue("MyKeyName", "Hello EricSun." + DateTime.Now.ToString()); } } else { key.SetValue("MyKeyName", "Hello EricSun." + DateTime.Now.ToString()); } }
//2.读取信息(IIS也可以正常读取)
protected string GetRegistryValue(string path, string paramName) { string value = string.Empty; RegistryKey root = Registry.LocalMachine; RegistryKey rk = root.OpenSubKey(path); if (rk != null) { value = (string)rk.GetValue(paramName, null); } return value; }
string value = GetRegistryValue("SOFTWARE\\EricSun\\MyTestKey", "MyKeyName");
Response.Write(value);
|
请发表评论