FrmCreateLicence.cs 10.1 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Xml;
using System.IO;
using System.Runtime.InteropServices;

namespace CreateLicence
{
    public partial class FrmCreateLicence : Form
    {
        private string path="";
        public FrmCreateLicence()
        {
            InitializeComponent();

            Init();
        }

        private void Init()
        {
            path=System.Environment.CurrentDirectory;
            this.DTPTo.Value = DateTime.Now.AddYears(1);
            this.cmbProductName.SelectedIndex = 0;
            this.txtCurSerialNo.Text = LicenceMake.GetDiskID();
        }

        private bool CheckValidity()
        {
            if (String.IsNullOrEmpty(this.txtVerify.Text.Trim()))
            {
                MessageBox.Show("请输入硬盘序列号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtVerify.Focus();
                return false;
            }
            if (String.IsNullOrEmpty(this.txtCompanyName.Text.Trim()))
            {
                MessageBox.Show("请输入公司名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtCompanyName.Focus();
                return false;
            }
            if (String.IsNullOrEmpty(this.cmbProductName.Text.Trim()))
            {
                MessageBox.Show("请输入产品名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.cmbProductName.Focus();
                return false;
            }
            if (String.IsNullOrEmpty(this.txtVersionNumber.Text.Trim()))
            {
                MessageBox.Show("请输入版本号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtVersionNumber.Focus();
                return false;
            }
            if (DTPFrom.Value.Date > DTPTo.Value.Date)
            {
                MessageBox.Show("授权使用期限设置不当!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.DTPTo.Focus();
                return false;
            }
            if (String.IsNullOrEmpty(this.txtFilePath.Text.Trim()))
            {
                MessageBox.Show("请输入Licence文件的存放路径!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtFilePath.Focus();
                return false;
            }

            try
            {
                string userCount = this.txtUserCount.Text.Trim();
                if (!String.IsNullOrEmpty(userCount))
                {
                    Convert.ToInt32(userCount);
                }
                else
                {
                    MessageBox.Show("请输入用户数量!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.txtUserCount.SelectAll();
                    this.txtUserCount.Focus();
                    return false;
                }
            }
            catch
            {
                MessageBox.Show("用户数量为整数型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtUserCount.SelectAll();
                this.txtUserCount.Focus();
                return false;
            }

            return true;
        }

        private bool CheckDirectory()
        {
            try
            {
                if (Directory.Exists(txtFilePath.Text.Trim().Substring(0, txtFilePath.Text.Trim().LastIndexOf(@"\"))))
                    return true;

                if (MessageBox.Show("指定的路径不存在,是否要新建?", "创建许可证", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.No)
                {
                    return false;
                }

                Directory.CreateDirectory(txtFilePath.Text.Trim().Substring(0, txtFilePath.Text.Trim().LastIndexOf(@"\")));
                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show("无法创建指定的目录!\n" + e.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }
        }

        private bool CheckFile()
        {
            if (!File.Exists(txtFilePath.Text.Trim()))
                return true;

            if (MessageBox.Show("已存在名为" + txtFilePath.Text.Trim().Substring(
                txtFilePath.Text.Trim().LastIndexOf(@"\") + 1) + "的文件,是否要覆盖?", "创建许可证",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button1) == DialogResult.No)
            {
                return false;
            }

            File.Delete(txtFilePath.Text.Trim());
            return true;
        }

        private bool CreateLicenceFile()
        {
            LicenceMake licence = new LicenceMake();

            DataRow m_dr = licence.dataSet.Tables[LicenceMake.TABLENAME].NewRow();
            m_dr["DiskID"] = this.txtVerify.Text.Trim();
            m_dr["CompanyName"] = this.txtCompanyName.Text.Trim();
            m_dr["UserCount"] = this.txtUserCount.Text.Trim();
            m_dr["ProductName"] = this.cmbProductName.Text.Trim();
            m_dr["Version"] = this.txtVersionNumber.Text.Trim();
            m_dr["FromDate"] = DTPFrom.Value.ToShortDateString();
            m_dr["ToDate"] = DTPTo.Value.ToShortDateString();
            if (this.rbCVS.Checked == true)
                m_dr["ConfigueDepot"] = "CVS";
            else if (this.rbVSS.Checked == true)
                m_dr["ConfigueDepot"] = "VSS";
            else if (this.rbBoth.Checked == true)
                m_dr["ConfigueDepot"] = "CVS and VSS";
            licence.dataSet.Tables[LicenceMake.TABLENAME].Rows.Add(m_dr);

            return (licence.CreateLicenceFile(txtFilePath.Text.Trim())
                 && licence.LicenceList(path));
        }

        private void BtnPath_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Licence files (*.lic)|*.lic";
            dlg.FilterIndex = 0;
            dlg.InitialDirectory = Application.ExecutablePath;
            dlg.FileName = cmbProductName.Text.Trim() + ".lic";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txtFilePath.Text = dlg.FileName;
            }
        }

        private void BtnOK_Click(object sender, EventArgs e)
        {
            if (!CheckValidity())
                return;

            if (!CheckDirectory())
                return;

            if (!CheckFile())
                return;

            if (!CreateLicenceFile())
            {
                MessageBox.Show("在生成许可文件时出错!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            MessageBox.Show("你已成功生成一份" + txtFilePath.Text.Trim().Substring(txtFilePath.Text.Trim().LastIndexOf(@"\") + 1) + "许可证!");
            BtnCancel.Text = "完成";
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        #region Old
        /*

        private void BtnOK_Click(object sender, EventArgs e)
        {
            this.m_SavePath = this.txtFilePath.Text;
            if (CheckInfo())
            {
                CheckLicenceFile();
            }
        }

        private void CheckLicenceFile()
        {
            string ProductName = this.cmbProductName.Items[cmbProductName.SelectedIndex].ToString().Trim();
            if (m_SavePath == "")
            {
                m_SavePath = Application.ExecutablePath;
                if (m_SavePath.EndsWith(@"\"))
                    m_SavePath += @"\";
                m_SavePath = m_SavePath.Substring(0, m_SavePath.LastIndexOf(@"\"));
                m_SavePath += ProductName + ".lic";
            }
            
            CreateLicenceInfo();
        }

        private void CreateLicenceInfo()
        {
            string Verify = this.txtVerify.Text.Trim();
            string CompanyName = this.txtCompanyName.Text.Trim();
            string UserCount = this.txtUserCount.Text.Trim();
            string ProductName = this.cmbProductName.Items[cmbProductName.SelectedIndex].ToString().Trim();
            string VersionNumber = this.txtVersionNumber.Text.Trim();
            DateTime FromDate = Convert.ToDateTime(DTPFrom.Value);
            DateTime ToDate = Convert.ToDateTime(DTPTo.Value);
            string ConfigueDepot = "";
            if (this.rbCVS.Checked == true)
                ConfigueDepot = "CVS";
            else if (this.rbVSS.Checked == true)
                ConfigueDepot = "VSS";
            else if (this.rbBoth.Checked == true)
                ConfigueDepot = "CVS and VSS";

            LM = new LicenceMake();
            DataSet m_dsValue = LM.dataSet;
            m_dsValue.Tables[LM.TABLENAME].Clear();
            DataRow m_dr = m_dsValue.Tables[LM.TABLENAME].NewRow();
            m_dr["DiskID"] = Verify;
            m_dr["CompanyName"] = CompanyName;
            m_dr["UserCount"] = UserCount;
            m_dr["ProductName"] = ProductName;
            m_dr["Version"] = VersionNumber;
            m_dr["FromDate"] = FromDate.ToShortDateString();
            m_dr["ToDate"] = ToDate.ToShortDateString();
            m_dr["ConfigueDepot"] = ConfigueDepot;
            m_dsValue.Tables[LM.TABLENAME].Rows.Add(m_dr);
            //LM.RSAKey = m_Txt;
            if (LM.CreateLicenceFile(m_dsValue, m_SavePath))
            {
                FinishTask();
            }
            else
            {
                MessageBox.Show("在生成许可文件时出错!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
        }

        private void FinishTask()
        {
            string ProductName = this.ProductName;
            MessageBox.Show("你已成功生成一份" + m_SavePath.Substring(m_SavePath.LastIndexOf(@"\") + 1) + "许可证!");
            BtnCancel.Text = "完成";
        }
        */
        #endregion
    }
}