1、添加contextMenuStrip1控件
2、右键打开功能
private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e) { //文件没有选中 if (this.listView1.SelectedItems.Count == 0) return; //获取选中文件 var selectedItem = this.listView1.SelectedItems[0]; string name = selectedItem.SubItems[0].Text; try { DataTable dt = BLL.ProData.GetData(BillID, name); string FileName = Application.StartupPath + "\\" + name; Util.Pub.Bytes2File((byte[])dt.Rows[0]["Data"], FileName); if (File.Exists(FileName)) { try { Process.Start(FileName); } catch (Exception ex) { throw ex; } } else { DialogResult dg = MessageBox.Show("文件不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); if (dg == DialogResult.Yes) { } } } catch (Exception msg) //异常处理 { MessageBox.Show(msg.Message); } }
2、右键下载功能
private void 下载文件ToolStripMenuItem_Click(object sender, EventArgs e) { try { //文件没有选中 if (this.listView1.SelectedItems.Count == 0) return; //获取选中文件 var selectedItem = this.listView1.SelectedItems[0]; string name = selectedItem.SubItems[0].Text; DataTable dt = BLL.ProData.GetData(BillID, name); if (dt.Rows.Count == 0) { Util.Msg.Error("找不到文件"); return; } SaveFileDialog sfd = new SaveFileDialog(); sfd.FileName = name; string[] ss = sfd.FileName.Split('.'); if (ss.Length > 1) { sfd.Filter = ss[ss.Length - 1] + "文件|*." + ss[ss.Length - 1]; } if (sfd.ShowDialog() == DialogResult.OK) { Util.Pub.BytesToFile((byte[])dt.Rows[0]["Data"], sfd.FileName); if (Util.Msg.Confirm("文件已下载,是否打开?")) { Process.Start(sfd.FileName); } } } catch (Exception ex) { Util.Msg.Error(ex.Message); } }3、右键删除功能
private void 删除文件ToolStripMenuItem_Click(object sender, EventArgs e) { //文件没有选中 if (this.listView1.SelectedItems.Count == 0) return; //获取选中文件 var selectedItem = this.listView1.SelectedItems[0]; string name = selectedItem.SubItems[0].Text; DataTable dt = BLL.ProData.GetData(BillID, name); string FileName = Application.StartupPath + "\\" + name; Util.Pub.Bytes2File((byte[])dt.Rows[0]["Data"], FileName); try { if (MessageBox.Show("确认要删除该文件?", "提示", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { //删除文件 Delete(FileName, true); BLL.ProData.Delete(BillID, name); //移除文件 foreach (ListViewItem item in this.listView1.SelectedItems) { this.listView1.Items.Remove(item); } MessageBox.Show(this, "成功删除了文件!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception msg) //异常处理 { MessageBox.Show(msg.Message); } }4、读取数据库文件加载到listview控件中
DataTable dtPatch = BLL.ProData.GetData(BillID); if (dtPatch.Rows.Count > 0) { listView1.SmallImageList = imageList1;
for (int i = 0; i < dtPatch.Rows.Count; i++) { string loadName = dtPatch.Rows[i]["FileName"].ToString(); string ex = loadName.Substring(loadName.IndexOf('.'));
if (!imageList1.Images.Keys.Contains(ex)) { imageList1.Images.Add(ex, Icon.ExtractAssociatedIcon(loadName)); } ListViewItem lvi = new ListViewItem(); lvi.Text = loadName; lvi.ImageIndex = imageList1.Images.Keys.IndexOf(ex); listView1.Items.Add(lvi); } }