Beispiel #1
0
        /// <summary>
        /// 读取注册文件
        /// </summary>
        /// <param name="path">文件路径</param>
        public static clsRegInfo LoadRegFile(string path)
        {
            StreamReader sr      = new StreamReader(path, Encoding.UTF8);
            string       regInfo = sr.ReadToEnd();

            sr.Close();
            string ID_8 = regInfo.Substring(0, 8);

            regInfo = regInfo.Substring(8, regInfo.Length - 8);
            regInfo = DESDecrypt(regInfo, ID_8);
            if (regInfo.Substring(0, 8) != ID_8)
            {
                return(null);
            }
            regInfo = regInfo.Substring(8, regInfo.Length - 8);
            byte[] reg = new byte[regInfo.Length / 2];
            for (int i = 0; i < regInfo.Length / 2; i++)
            {
                reg[i] = Convert.ToByte(regInfo.Substring(2 * i, 2), 16);
            }
            BinaryFormatter binFormat = new BinaryFormatter();
            Stream          sm        = new MemoryStream(reg);
            clsRegInfo      ret       = (clsRegInfo)binFormat.Deserialize(sm);

            sm.Close();
            return(ret);
        }
Beispiel #2
0
        /// <summary>
        /// 生成注册文件
        /// </summary>
        /// <param name="RegInfo">注册信息</param>
        /// <param name="path">导出路径</param>
        public static bool ExportRegFile(clsRegInfo RegInfo, string path)
        {
            try
            {
                Stream          fStream   = new FileStream(Environment.GetEnvironmentVariable("TEMP", EnvironmentVariableTarget.Machine) + "\\tmpRegInfo.tmp", FileMode.Create, FileAccess.ReadWrite);
                BinaryFormatter binFormat = new BinaryFormatter();//创建二进制序列化器

                binFormat.Serialize(fStream, RegInfo);
                fStream.Seek(0, SeekOrigin.Begin);
                StringBuilder ret = new StringBuilder();
                while (fStream.Position != fStream.Length)
                {
                    ret.AppendFormat("{0:X2}", fStream.ReadByte());
                }
                string regInfo = ret.ToString();
                fStream.Close();
                File.Delete(Environment.GetEnvironmentVariable("TEMP", EnvironmentVariableTarget.Machine) + "\\tmpRegInfo.tmp");
                string ID_8 = RegInfo.ID.Substring(0, 8);
                regInfo = ID_8 + regInfo;
                regInfo = DESEncrypt(regInfo, ID_8);
                regInfo = ID_8 + regInfo;
                StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8);
                sw.Write(regInfo);
                sw.Close();
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }