FrmCreateLicence.cs
10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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
}
}