需要引入命名空間:
using Microsoft.Win32;
/// 讀注冊表中指定鍵的值
/// </summary>
/// <param name="key">鍵名</param>
/// <returns>返回鍵值</returns>
public static string ReadReg(string key)
{
string temp = "";
try
{
RegistryKey key = Registry.LocalMachine; //Registry參數自己選擇
RegistryKey subKey = Key.OpenSubKey("SOFTWARE\Exobo"); //注冊表位置
temp = subKey.GetValue(key).ToString();
subKey.Close();
key .Close();
return temp;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 創建目錄(第一次注冊)---寫入
/// </summary>
/// <param name="username"></param>
/// <param name="password"></param>
/// <param name="userType">版本類型</param>
/// <param name="userMax">最大試用次數</param>
/// <param name="userTime">當前試用次數</param>
/// <returns></returns>
public static bool CreateReg(string username,string password,string userType,string userMax,string userTime)
{
bool temp = false;
try
{
RegistryKey Key = Registry.LocalMachine;
RegistryKey subKey = Key.OpenSubKey(@"SOFTWAREExobo");
//未注冊 從新創建
if (subKey == null)
{
Key.CreateSubKey("SOFTWARE\Exobo");
}
//修改注冊信息
subKey = Key.OpenSubKey("SOFTWARE\Exobo", true);
subKey.SetValue(username, password);
subKey.SetValue(username + "Type", userType);
subKey.SetValue(username + "Time", userTime);
subKey.SetValue(username + "Max", userMax);
temp = true;
subKey.Close();
Key.Close();
return temp;
}
catch (Exception)
{
throw;
}
}
//判段是否有該項
public static bool HaveReg()
{
bool temp = false;
try
{
RegistryKey Key = Registry.LocalMachine;
RegistryKey subKey = Key.OpenSubKey(@"SOFTWAREExobo");
//未注冊 從新創建
if (subKey == null)
{
temp = false;
}
else
{
temp = true;