private void btn_AddNew_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = string.IsNullOrEmpty(oldPath) ? "C:\\" : oldPath; //初始化查询路径 ofd.Multiselect = true; if (ofd.ShowDialog(this) == DialogResult.OK) { foreach (string fn in ofd.FileNames) { MailAccessoriesHelper mbh = new MailAccessoriesHelper(); mbh.FileFullName = fn; mbh.FileName = fn.Substring(fn.LastIndexOf("\\") + 1); FileInfo fi = new FileInfo(fn); mbh.FileSize = fi.Length.ToString(); this._mailAccessorList.Add(mbh); //操作附件 if (!string.IsNullOrEmpty(mbh.FileFullName)) { string sfdir = this._ServerSavePath + "\\" + this._invoiceType + "\\" + this._invoiceid; try { System.IO.Directory.CreateDirectory(sfdir); } catch (Exception ex) { throw new Helper.MessageValueException(ex.Message); } System.IO.File.Copy(fn, sfdir + "\\" + fn.Substring(fn.LastIndexOf("\\") + 1), true); } } oldPath = ofd.FileName; } ConstructAccessoriesData(); }
//点击删除,删除文件 private void repositoryItemHyperLinkEdit2_Click(object sender, EventArgs e) { MailAccessoriesHelper mah = this.bindingSource1.Current as MailAccessoriesHelper; if (DialogResult.OK == MessageBox.Show("是否刪除附件:" + mah.FileName, "提示", MessageBoxButtons.OKCancel)) { System.IO.File.Delete(mah.FileFullName); (this.bindingSource1.DataSource as IList <MailAccessoriesHelper>).Remove(mah); this.gridControl1.RefreshDataSource(); } }
//下载文件 private void repositoryItemHyperLinkEdit3_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.RootFolder = Environment.SpecialFolder.Desktop; fbd.Description = "請選擇保存目錄"; if (DialogResult.OK == fbd.ShowDialog(this)) { string saveFolder = fbd.SelectedPath; MailAccessoriesHelper mah = this.bindingSource1.Current as MailAccessoriesHelper; if (System.IO.File.Exists(saveFolder + "\\" + mah.FileName)) { MessageBox.Show("該文件在該目錄已存在!", this.Text, MessageBoxButtons.OK); return; } System.IO.File.Copy(mah.FileFullName, saveFolder + "\\" + mah.FileName); MessageBox.Show("已下載至:" + saveFolder + "\\" + mah.FileName, this.Text, MessageBoxButtons.OK); } }
//获得所有附件列表 private void ConstructAccessoriesData() { this._mailAccessorList.Clear(); //查看附件,通过主键路径查找,如果没有则数据为空 if (Directory.Exists(this._ServerSavePath + "\\" + this._invoiceType + "\\" + this._invoiceid)) { string[] filenames = Directory.GetFiles(this._ServerSavePath + "\\" + this._invoiceType + "\\" + this._invoiceid); foreach (string fn in filenames) { MailAccessoriesHelper mah = new MailAccessoriesHelper(); mah.FileName = fn.Substring(fn.LastIndexOf("\\") + 1); //这里直接使用文件名.不包含路径 mah.FileFullName = fn; FileInfo fi = new FileInfo(mah.FileFullName); mah.FileSize = fi.Length.ToString(); mah.FileStatus = AttachmentStatus.Normal; this._mailAccessorList.Add(mah); } } this.bindingSource1.DataSource = this._mailAccessorList; this.gridControl1.RefreshDataSource(); }
//点击文件名称打开文件 private void repositoryItemHyperLinkEdit1_Click(object sender, EventArgs e) { MailAccessoriesHelper mah = this.bindingSource1.Current as MailAccessoriesHelper; System.Diagnostics.Process.Start(mah.FileFullName); }