您的位置:首页 > 编程语言 > C#

c#写入注册表,读取注册表。使用二进制数据

2007-08-02 01:35 656 查看
c#写入

private void button4_Click(object sender, EventArgs e)
{
try
{
RegistryKey a = Registry.LocalMachine;
RegistryKey b = a.OpenSubKey("SOFTWARE",true);
RegistryKey c = b.CreateSubKey("dsoa\\regdate", RegistryKeyPermissionCheck.ReadWriteSubTree);
c.SetValue("date", charToBinary(System.DateTime.Now.ToShortDateString()), RegistryValueKind.Binary);

}
catch (Exception eq)
{
MessageBox.Show(eq.ToString());
}
}

#region 把字符串转化为二进制
private byte[] charToBinary(string str)
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] tag = encoding.GetBytes(str);
return tag;
}
#endregion

读取

try
{
RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\dsoa\regdate");

string value = ConvertBytes((byte[])regkey.GetValue("date"));
DateTime dt1 = Convert.ToDateTime(value);
System.TimeSpan dayspan = System.DateTime.Today - dt1;
BLL.Login login=new OASolution.BLL.Login();
if (dayspan.Days >= 31 || login.GetCurrentIsExpire() == false)
{
Page.Response.Redirect("end.htm",false);
}

}
catch (Exception eq)
{
Page.Response.Redirect("instalerror.htm", false);

}

#region 由字节转化为字符串
private string ConvertBytes(byte[] data)
{
ASCIIEncoding encoding = new ASCIIEncoding();
Char[] dataChars = encoding.GetChars(data);

// 使用StringBuilder来转化成字符串
StringBuilder builder = new StringBuilder();
builder.Append(dataChars);

// 得到字符串
string dataString = builder.ToString();

// 提取左右两边的空格
dataString = dataString.Trim();
// 返回
return dataString;

}
#endregion

本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: