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("磁盘添加失败!"); } } }