using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace exp14 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.Text = "异常捕获及处理"; label1.Text = "源 文 件"; label2.Text = "目标文件夹"; label3.Text = "目标文件名"; label4.Visible = false; button1.Text = "浏览..."; button2.Text = "复制"; button3.Text = "浏览..."; button4.Text = "退出"; textBox3.Text = "1.txt"; } private void button1_Click(object sender, EventArgs e) { openFileDialog1.InitialDirectory = "D://C#"; openFileDialog1.FileName = ""; openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog1.FilterIndex = 1; //openFileDialog1.RestoreDirectory = false ; if (openFileDialog1.ShowDialog() == DialogResult.OK) { textBox1.Text = openFileDialog1.FileName; } } private void button2_Click(object sender, EventArgs e) { label4.Visible = true; try { string str = textBox2.Text; str = str.Substring((str.Length) -1,1); if (str == "//") { File.Copy(textBox1.Text, textBox2.Text + textBox3.Text); } else { File.Copy(textBox1.Text, textBox2.Text +"//"+ textBox3.Text); } label4.Text = "文件复制成功!"; } catch (FileNotFoundException) { label4.Text = "文件不存在!"; } catch (ArgumentException) { label4.Text = "参数错误!"; } catch (IOException) { label4.Text = "缺少目标文件名、文件夹不存在或目标文件已存在"; } catch { label4.Text = "出现其他错误"; } finally { MessageBox.Show("感谢使用本软件!", "程序结束", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } private void button3_Click(object sender, EventArgs e) { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { textBox2.Text = folderBrowserDialog1.SelectedPath ; } } private void button4_Click(object sender, EventArgs e) { this.Close(); } } }