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 File类操作 { public partial class Form1 : Form { private string strFileName; public Form1() { InitializeComponent(); //两个按钮的单击事件关联到同一个方法 this.btnCopy.Click += this.move_copy; this.btnMove.Click += this.move_copy; } //判断文件是否存在 private bool CheckSource() { if (strFileName == "" || !File.Exists(strFileName)) { MessageBox.Show("请选择文件!"); return false; } else { return true; } } private void btnSelect_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "(文本文件*.txt)|*.txt|(所有文件*.*)|*.*"; DialogResult result = ofd.ShowDialog(); if (result == DialogResult.OK) { this.strFileName = ofd.FileName; txtFile.Text = this.strFileName; //创建一个获取文件信息的对象 FileInfo fileInfo = new FileInfo(this.strFileName); string creationTime = fileInfo.CreationTime.ToString(); long size = fileInfo.Length; string name = fileInfo.Name; string extenison = fileInfo.Extension; string fullname = fileInfo.FullName; string fileMessage = string.Format("文件名:{0}/r/n文件的完整目录:{1}/r/n文件扩展名:{2}/r/n文件创建时间:{3}/r/n文件大小:{4}Bytes/r/n", name,fullname,extenison,creationTime,size); txtInfo.Text = fileMessage; } } private void move_copy(object sender, EventArgs e) { if (!CheckSource()) { return; } //判断用户当前选择的是哪个按钮(复制按钮与移动按钮) Button curBtn = sender as Button; //打开目录浏览对话框 FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == DialogResult.OK) { DialogResult result = MessageBox.Show("确定要执行这个操作吗?", "确认对话框", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information); if (result == DialogResult.Yes) { //fbd.SelectedPath获取你选择的目录路径 //Path.GetFileName(this.strFileName)获取你操作的文件的文件名和扩展名 //strDesFile就是新的完整路径名 string strDesFile = fbd.SelectedPath + @"/" + Path.GetFileName(this.strFileName); try { if (curBtn.Text == "移动") { File.Move(strFileName, strDesFile); } if (curBtn.Text == "复制") { File.Copy(strFileName,strDesFile); } txtInfo.Clear(); if (File.Exists(strDesFile)) { string fileMessage = string.Format("文件" + this.strFileName + "/r/n已成功" + curBtn.Text + "/r/n" + strDesFile); txtInfo.Text = fileMessage; } else { txtInfo.Text = strDesFile + curBtn.Text + "出错!/r/n"; } } catch(Exception err) { txtInfo.Text="出错:"+err.Message+"/r/n"; } } } } private void btnDelete_Click(object sender, EventArgs e) { try { File.Delete(strFileName); txtInfo.Text = strFileName + " 删除成功!"; } catch (Exception err) { MessageBox.Show(err.Message); } } private void bbtnCreateDir_Click(object sender, EventArgs e) { //创建目录 Directory.CreateDirectory(@"c:/霸天虎/霸天虎的资料夹"); Directory.CreateDirectory(@"c:/霸天虎/霸天虎的相片夹"); //创建文件,该文件时打开的,如果要删除,则必须先关闭 FileStream f1= File.Create(@"c:/霸天虎/霸天虎的资料夹/我的个人信息.doc"); FileStream f2 = File.Create(@"c:/霸天虎/霸天虎的资料夹/我的好友信息.txt"); FileStream f3 = File.Create(@"c:/霸天虎/霸天虎的相片夹/北京之行.jpg"); FileStream f4 = File.Create(@"c:/霸天虎/霸天虎的相片夹/九江庐山之行.jpg"); f1.Close(); f2.Close(); f3.Close(); f4.Close(); //得到指定目录下的所有文件 string[] dirs = Directory.GetDirectories(@"c:/霸天虎"); string[] files1 = Directory.GetFiles(@"c:/霸天虎/霸天虎的资料夹"); string[] files2 = Directory.GetFiles(@"c:/霸天虎/霸天虎的相片夹"); txtInfo.Text = @"c:/霸天虎 该目录下包含的目录有:"; foreach (string s in dirs) { txtInfo.Text+=s+" "; } txtInfo.Text+="/r/nc://霸天虎//霸天虎的资料夹 该目录下包含的文件有:"; foreach(string s in files1) { txtInfo.Text+=s+" "; } txtInfo.Text+="/r/nc://霸天虎//霸天虎的相片夹 该目录下包含的文件有:"; foreach(string s in files1) { txtInfo.Text+=s+" "; } try { Directory.Delete(@"c:/霸天虎/霸天虎的资料夹", true); MessageBox.Show("删除成功!"); } catch (Exception err) { //如果f1、f2、f3、f4没有执行close()方法,则会出现异常 MessageBox.Show(err.Message); } } } }