zoukankan      html  css  js  c++  java
  • c#写入注册表,读取注册表。使用二进制数据

    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注册。

  • 相关阅读:
    kNN算法python实现和简单数字识别的方法
    python3.4学习笔记(二十) python strip()函数 去空格 函数的用法
    SolrCloud分布式集群部署步骤
    hbases索引技术:Lily HBase Indexer介绍
    HBase1.0以上版本的API改变
    Hbase1.0 客户端api
    java.io.IOException: Could not locate executable nullinwinutils.exe in the Hadoop binaries
    Resharp最新破解方法
    Cloudera Manager 5和CDH5离线安装
    Apache Thrift
  • 原文地址:https://www.cnblogs.com/zjypp/p/2319477.html
Copyright © 2011-2022 走看看