using CommonLang;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CommonFroms
{
public static class RegistUtils
{
///
/// 从注册表里读取信息
///
///
///
///
///
public static bool TryGetAppRegistry(string key, out T value)
{
return TryGetAppRegistry(null, key, out value);
}
///
/// 在注册表里写入信息
///
///
///
///
public static void PutAppRegistry(string key, T value)
{
PutAppRegistry(null, key, value);
}
///
/// 从注册表里读取信息
///
///
/// 应用子目录
///
///
///
public static bool TryGetAppRegistry(string path, string key, out T value)
{
RegistryKey masterKey = Registry.CurrentUser.CreateSubKey("SOFTWARE\\" + Application.CompanyName + "\\" + Application.ProductName + ((path != null) ? ("\\" + path) : ("")));
try
{
if (masterKey != null)
{
object obj = masterKey.GetValue(key);
if (obj != null)
{
value = (T)Parser.StringToObject(obj.ToString(), typeof(T));
return true;
}
}
}
finally
{
masterKey.Close();
}
value = default(T);
return false;
}
///
/// 在注册表里写入信息
///
///
/// 应用子目录
///
///
public static void PutAppRegistry(string path, string key, T value)
{
RegistryKey masterKey = Registry.CurrentUser.CreateSubKey("SOFTWARE\\" + Application.CompanyName + "\\" + Application.ProductName + ((path != null) ? ("\\" + path) : ("")));
try
{
if (masterKey != null)
{
masterKey.SetValue(key, Parser.ObjectToString(value));
}
}
finally
{
masterKey.Close();
}
}
}
}