forked from wangyx0055/DisklessSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VdiskTemplateAdd.cs
181 lines (153 loc) · 6.48 KB
/
VdiskTemplateAdd.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
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 System.Management;
using Win2012WMIiSCSIVhdxManager;
namespace NoDiskSystem
{
public partial class VdiskTemplateAdd : Form
{
public List<VdiskTemplate> vdiskTempletList;
private DataGridView parentDataGridView;
string iscsiURL;
string username;
string password;
public DataGridView ParentDataGridView
{
get { return parentDataGridView; }
set { parentDataGridView = value; }
}
public List<VdiskTemplate> VdiskTemplet
{
get { return vdiskTempletList; }
set { vdiskTempletList = value; }
}
public VdiskTemplateAdd()
{
InitializeComponent();
}
private void 取消button_Click(object sender, EventArgs e)
{
this.Close();
}
private void 确定button_Click(object sender, EventArgs e)
{
if (this.磁盘名称textBox.Text.Replace(" ", "") == "" || this.磁盘容量textBox.Text.Replace(" ", "") == "" || this.映像文件textBox.Text.Replace(" ", "") == "")
{
MessageBox.Show("请填写完整的信息!");
return;
}
if (!TextInputStringValidator.IsEnglishWithNum(this.磁盘名称textBox.Text.Replace(" ", "")))
{
MessageBox.Show("磁盘名需以英文字母、数字、_组成!");
return;
}
if (!TextInputStringValidator.IsNumber(this.磁盘容量textBox.Text.Replace(" ", "")))
{
MessageBox.Show("磁盘大小只能为数字!");
return;
}
string diskName = this.磁盘名称textBox.Text.Replace(" ", "");
int diskSize = int.Parse(this.磁盘容量textBox.Text.Replace(" ", ""));
string diskPath = this.映像文件textBox.Text.Replace(" ", "");
int diskType;
if (this.稀疏文件checkBox.Checked)
{
diskType = 3;
}
else
{
diskType = 2;
}
if (diskSize < 1 || diskSize > 2048)
{
MessageBox.Show("磁盘大小应介于1-2048G之间!");
return;
}
for (int i = 0; i < this.vdiskTempletList.Count; i++)
{
if (vdiskTempletList[i].DiskName == diskName)
{
MessageBox.Show("磁盘名称已存在!");
return;
}
}
var dic = new Dictionary<string, object>();
dic["disk_name"] = diskName;
dic["disk_size"] = diskSize;
dic["disk_path"] = diskPath + "\\";
dic["disk_type"] = diskType;
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);
parentDataGridView.AutoGenerateColumns = false;
parentDataGridView.DataSource = vdiskTempletList.ToArray();
//创建磁盘文件
DiskManagementData ParamObj = new DiskManagementData(); // TODO: 初始化为适当的值
if (this.稀疏文件checkBox.Checked)
{
ParamObj.VhdxType = 3; //动态盘
}
else
{
ParamObj.VhdxType = 2; //固定大小
}
ParamObj.DevicePath = dic["disk_path"].ToString() + dic["disk_name"].ToString() + ".vhdx";
ParamObj.TargetName = dic["disk_name"].ToString();
ParamObj.DiskSize = ushort.Parse(dic["disk_size"].ToString());
ParamObj.TargetIQN = "HstecsTemplet." + dic["disk_name"].ToString();
ParamObj.ParentPath = dic["disk_path"].ToString() + dic["disk_name"].ToString() + ".vhdx";
ParamObj.serverURL = iscsiURL;
ParamObj.Username = username;
ParamObj.Password = password;
bool actual;
actual = DiskManager.CreateVhdxDisk(ParamObj);
if (actual == true)
{
MessageBox.Show("磁盘添加成功!");
}
else
{
MessageBox.Show("磁盘添加失败!");
}
conn.Close();
this.Close();
}
}
}
private void 映像文件button_Click(object sender, EventArgs e)
{
FolderBrowserDialog path = new FolderBrowserDialog();
path.ShowDialog();
this.映像文件textBox.Text = path.SelectedPath;
}
private void VdiskTemplateAdd_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", "");
}
}
}