-
Notifications
You must be signed in to change notification settings - Fork 14
/
VdiskTemplateManageWindow.cs
306 lines (246 loc) · 12.1 KB
/
VdiskTemplateManageWindow.cs
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;
using Win2012WMIiSCSIVhdxManager;
using System.Management;
namespace NoDiskSystem
{
public partial class VdiskTemplateManageWindow : Form
{
List<VdiskTemplate> vdiskTempletList = new List<VdiskTemplate>();
string iscsiURL;
string username;
string password;
public VdiskTemplateManageWindow()
{
InitializeComponent();
this.磁盘管理dataGridView.RowHeadersVisible = false;
}
private void VdiskTemplateManageWindow_Load(object sender, EventArgs e)
{
IniFile ini = new IniFile(Environment.CurrentDirectory + "\\setting.ini");
iscsiURL = ini.ReadString("IscsiServer", "URL", "");
username = ini.ReadString("IscsiServer", "USERNAME", "");
password = ini.ReadString("IscsiServer", "PASSWORD", "");
using (SQLiteConnection conn = new SQLiteConnection("data source=nodisk.db"))
{
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = conn;
conn.Open();
SQLiteHelper sh = new SQLiteHelper(cmd);
DataTable dt = sh.Select("select * from VDISK_TEMPLET;");
VdiskTemplate vdiskTemp;
foreach (DataRow row in dt.Rows)
{
vdiskTemp = new VdiskTemplate();
vdiskTemp.DiskId = row["disk_id"].ToString();
vdiskTemp.DiskName = row["disk_name"].ToString();
vdiskTemp.DiskSize = (int)row["disk_size"];
vdiskTemp.DiskPath = row["disk_path"].ToString();
vdiskTemp.DiskType = (int)row["disk_type"];
vdiskTempletList.Add(vdiskTemp);
}
this.磁盘管理dataGridView.AutoGenerateColumns = false;
this.磁盘管理dataGridView.DataSource = vdiskTempletList;
conn.Close();
}
}
}
private void 新增button_Click(object sender, EventArgs e)
{
VdiskTemplateAdd vdiskTempletAdd = new VdiskTemplateAdd();
vdiskTempletAdd.vdiskTempletList = this.vdiskTempletList;
vdiskTempletAdd.ParentDataGridView = this.磁盘管理dataGridView;
vdiskTempletAdd.ShowDialog();
}
private void 删除button_Click(object sender, EventArgs e)
{
bool hasClientUseTheDisk = false;
bool hasPointOfTheDisk = false;
string diskIds = this.磁盘管理dataGridView.SelectedRows[0].Cells["diskId"].Value.ToString();
using (SQLiteConnection conn = new SQLiteConnection("data source=nodisk.db"))
{
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = conn;
conn.Open();
SQLiteHelper sh = new SQLiteHelper(cmd);
DataTable dtClientUseDisk = sh.Select("select * from CLIENT_DISK_LIST where disk_id='" + diskIds + "';");
DataTable dtPointOfTheDisk = sh.Select("select * from VDISK_RESTORE_POINT where disk_id='" + diskIds + "';");
if (dtClientUseDisk.Rows.Count > 0)
{
hasClientUseTheDisk = false;
}
else {
hasClientUseTheDisk = true;
}
if (dtPointOfTheDisk.Rows.Count > 0)
{
hasPointOfTheDisk = false;
}
else
{
hasPointOfTheDisk = true;
}
conn.Close();
}
}
if (!hasClientUseTheDisk)
{
MessageBox.Show("存在使用本磁盘的工作站,请先将工作站磁盘设置为空或设置为其他工作站!");
return;
}
if (!hasPointOfTheDisk)
{
MessageBox.Show("本磁盘存在还原点,请先将所有还原点删除后再删除本磁盘!");
return;
}
if (vdiskTempletList.Count != 0)
{
MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
DialogResult dr = MessageBox.Show("确定要删除选中的磁盘吗?", "删除磁盘", messButton);
if (dr == DialogResult.OK)
{
//删除选中的磁盘
using (SQLiteConnection conn = new SQLiteConnection("data source=nodisk.db"))
{
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = conn;
conn.Open();
SQLiteHelper sh = new SQLiteHelper(cmd);
//int at = this.skinDataGridViewVdiskAdd.CurrentRow.Index;
string diskId = this.磁盘管理dataGridView.SelectedRows[0].Cells["diskId"].Value.ToString();
string diskName = this.磁盘管理dataGridView.SelectedRows[0].Cells["diskName"].Value.ToString();
string diskPath = this.磁盘管理dataGridView.SelectedRows[0].Cells["diskPath"].Value.ToString();
string deleteSql = "DELETE from VDISK_TEMPLET WHERE disk_id = '" + diskId + "'";
for (int i = 0; i < vdiskTempletList.Count; i++)
{
if (vdiskTempletList[i].DiskId == diskId)
{
vdiskTempletList.RemoveAt(i);
}
}
this.磁盘管理dataGridView.AutoGenerateColumns = false;
this.磁盘管理dataGridView.DataSource = vdiskTempletList.ToArray();
sh.ExecuteScalar(deleteSql);
DiskManagementData ParamObj = new DiskManagementData(); // TODO: 初始化为适当的值
ParamObj.DevicePath = diskPath + diskName + ".vhdx";
ParamObj.TargetName = diskName;
ParamObj.serverURL = iscsiURL;
ParamObj.Username = username;
ParamObj.Password = password;
bool actual;
actual = DiskManager.RemoveDisk(ParamObj);
if (actual == true)
{
MessageBox.Show("磁盘删除成功!");
}
conn.Close();
}
}
}
else
{
return;
}
}
else
{
MessageBox.Show("系统中没有磁盘!");
}
}
private void 导入button_Click(object sender, EventArgs e)
{
string devicePath = "";
string fileName = "";
string fileNameWithOutEx = "";
string fileDirectoryName = "";
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
fileName = System.IO.Path.GetFileName(dialog.FileName);
fileNameWithOutEx = System.IO.Path.GetFileNameWithoutExtension(dialog.FileName);
devicePath = System.IO.Path.GetFullPath(dialog.FileName);
fileDirectoryName = System.IO.Path.GetDirectoryName(dialog.FileName) + "\\";
for (int i = 0; i < this.vdiskTempletList.Count; i++)
{
if (vdiskTempletList[i].DiskName == fileNameWithOutEx)
{
MessageBox.Show("磁盘名称已存在!");
return;
}
}
DiskManagementData ParamObj = new DiskManagementData(); // TODO: 初始化为适当的值
ParamObj.DevicePath = devicePath;
ParamObj.TargetName = fileNameWithOutEx;
ParamObj.TargetIQN = "HstecsTemplet." + fileNameWithOutEx;
ParamObj.serverURL = iscsiURL;
ParamObj.Username = username;
ParamObj.Password = password;
bool actual;
actual = DiskManager.ImportVhdxDisk(ParamObj);
if (actual == true)
{
//获取虚拟磁盘的类型和大小
ManagementObject DiskObj = null;
if (!DiskManager.GetVirtualDisk(@devicePath, ref DiskObj, iscsiURL, username, password))
{
throw new Exception(Extension.GetEnumDescription(ReturnCode.RC_GetDisk));
}
else
{
var dic = new Dictionary<string, object>();
dic["disk_name"] = fileNameWithOutEx;
dic["disk_size"] = uint.Parse(DiskObj.GetPropertyValue("Size").ToString()) / 1024;
dic["disk_path"] = fileDirectoryName;
dic["disk_type"] = uint.Parse(DiskObj.GetPropertyValue("Type").ToString());
dic["disk_id"] = System.Guid.NewGuid().ToString("N");
using (SQLiteConnection conn = new SQLiteConnection("data source=nodisk.db"))
{
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = conn;
conn.Open();
SQLiteHelper sh = new SQLiteHelper(cmd);
sh.Insert("VDISK_TEMPLET", dic);
VdiskTemplate vdiskTemplet = new VdiskTemplate();
vdiskTemplet.DiskId = dic["disk_id"].ToString();
vdiskTemplet.DiskName = dic["disk_name"].ToString();
vdiskTemplet.DiskSize = int.Parse(dic["disk_size"].ToString());
vdiskTemplet.DiskPath = dic["disk_path"].ToString();
vdiskTemplet.DiskType = int.Parse(dic["disk_type"].ToString());
vdiskTempletList.Add(vdiskTemplet);
this.磁盘管理dataGridView.AutoGenerateColumns = false;
this.磁盘管理dataGridView.DataSource = vdiskTempletList.ToArray();
conn.Close();
}
}
MessageBox.Show("磁盘添加成功!");
}
}
else
{
MessageBox.Show("磁盘添加失败!");
}
}
}
private void 退出button_Click(object sender, EventArgs e)
{
this.Close();
}
private void 还原点button_Click(object sender, EventArgs e)
{
VdiskRestorePointManageWindow vdiskRestorePointManageWindow = new VdiskRestorePointManageWindow();
vdiskRestorePointManageWindow.ShowDialog();
}
}
}