using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using Stone.Common; using System.IO; namespace Stone.WinModule.MyControl { public partial class MyFileEdit : UserControl { public string filecontent = ""; public MyFileEdit() { InitializeComponent(); } [CategoryAttribute(""), DescriptionAttribute("文件类型(*.btw)|*.btw")] public string Filter { get { return this.saveFileDialog1.Filter; } set { this.saveFileDialog1.Filter = value; this.openFileDialog1.Filter = value; } } private void btnSelect_Click(object sender, EventArgs e) { try { if (this.openFileDialog1.ShowDialog() == DialogResult.OK) { byte[] buff = File.ReadAllBytes(this.openFileDialog1.FileName); this.filecontent = Convert.ToBase64String(buff); this.txtFileName.Text = this.openFileDialog1.SafeFileName; } } catch (Exception ex) { MyMessageBox.ShowErrorMessage(ex.Message); } } private void btnSave_Click(object sender, EventArgs e) { try { if(this.txtFileName.Text.Trim() == "") { throw new Exception("当前没有文件"); } this.saveFileDialog1.FileName = this.txtFileName.Text.Trim(); if (this.saveFileDialog1.ShowDialog() == DialogResult.OK) { byte[] buff = Convert.FromBase64String(this.filecontent); File.WriteAllBytes(this.saveFileDialog1.FileName, buff); MyMessageBox.ShowInfoMessage(this.saveFileDialog1.FileName + " 另存成功"); } } catch (Exception ex) { MyMessageBox.ShowErrorMessage(ex.Message); } } private void btnDelete_Click(object sender, EventArgs e) { try { if (this.txtFileName.Text.Trim() == "") throw new Exception("当前没有可删除的文件"); if(MyMessageBox.ShowQuestion("是否要删除文件?")) { this.txtFileName.Text = ""; this.filecontent = ""; } }catch(Exception ex) { MyMessageBox.ShowErrorMessage(ex.Message); } } } }