Example #1
0
        public override License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions)
        {
            FileLicense   license = null;
            StringBuilder listMsg = new StringBuilder();

            if (context != null)
            {
                if (context.UsageMode == LicenseUsageMode.Designtime)
                {
                    license = new FileLicense(this, "");
                }
                if (license != null)
                {
                    return(license);
                }
                //licenses
                string licenseFile = GetlicFilePath();
                if (!string.IsNullOrWhiteSpace(licenseFile) && File.Exists(licenseFile))
                {
                    try
                    {
                        StreamReader sr     = new StreamReader(licenseFile, Encoding.Default);
                        String       xmlStr = DESEncrypt.Decrypt(sr.ReadToEnd());
                        var          entity = XmlFormatterSerializer.DeserializeFromXml <LicenseEntity>(xmlStr, typeof(LicenseEntity));
                        if (entity != null)
                        {
                            var pastDate = DateTime.Parse(entity.PastDate);
                            if (DateTime.Now > pastDate)
                            {
                                listMsg.AppendLine(" 许可证已过期");
                            }
                        }

                        if (listMsg.Length > 5)
                        {
                            listMsg.Insert(0, " 许可证编号:" + entity.ID);
                            listMsg.Insert(1, " 许可证名称:" + entity.Name);
                            listMsg.Insert(2, " 程序集版本号:" + entity.AssemblyName);
                            license = new FileLicense(this, listMsg.ToString());
                        }
                        else
                        {
                            license = new FileLicense(this, "");
                        }
                    }
                    catch (IOException e)
                    {
                        license = new FileLicense(this, "检查许可证失败" + e.Message);
                    }
                }
                else
                {
                    license = new FileLicense(this, "lic文件不存在,请联系供应商");
                }
            }
            return(license);
        }
Example #2
0
        private void but_Save_Click(object sender, EventArgs e)
        {
            if (txt_Name.Text.Length == 0)
            {
                MessageBox.Show("许可证名称不能为空!");
                return;
            }
            if (txt_AssemblyName.Text.Length == 0)
            {
                MessageBox.Show("程序集名称不能为空!");
                return;
            }
            but_Save.Enabled = false;
            LicenseEntity license = new LicenseEntity();

            license.ID             = Guid.NewGuid();
            license.Name           = txt_Name.Text;
            license.AssemblyName   = txt_AssemblyName.Text;
            license.PastDate       = txt_PastDate.Value.ToString("yyyy-MM-dd");
            license.UseNumber      = int.Parse(txt_UseNumber.Text);
            license.InstallNumber  = int.Parse(txt_InstallNumber.Text);
            license.VersionUpgrade = txt_VersionUpgrade.Checked;

            string context = DESEncrypt.Encrypt(XmlFormatterSerializer.SerializeToXml(license, license.GetType()));

            string licenseFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "LCL.lic");

            if (!File.Exists(licenseFile) == true)
            {
                File.Delete(licenseFile);
            }
            File.WriteAllText(licenseFile, context);

            MessageBox.Show("写入成功");
            but_Save.Enabled = true;
        }