教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 C#实现文件复制、移动与创建的实例代码

C#实现文件复制、移动与创建的实例代码

发布时间:2016-03-08   编辑:jiaochengji.com
为大家介绍用C#进行文件的复制、移动与创建的代码实例,有需要的朋友,可以参考学习下。

不多说,上代码,具体看注释,大家慢慢体会下实现思路。

//实现文件的复制、移动与创建
//by http://www.jbxue.com
protected void Page_Load(object sender, EventArgs e) 
{ 
if (!Page.IsPostBack) 
{ 
this.List(); 
}
} 
protected void Button1_Click(object sender, EventArgs e) 
{ 
if (TextBox2.Text == "") 
{ 
Response.Write("<script language=javascript>alert('文件名错误!');location='javascript:history.go(-1)'</script>"); 
} 
else 
{ 
try 
{ 
string path = Server.MapPath("File") + "\\" + TextBox2.Text + DropDownList1.Text; 
FileInfo fi = new FileInfo(path); 
if (!fi.Exists)//如果文件不存在 
{ 
fi.Create();//创建文件 
Label2.Text = "创建成功!文件名:" + TextBox2.Text + DropDownList1.Text; 
List(); 
} 
} 
catch (Exception error) 
{ 
Response.Write(error.ToString()); 
} 
} 
} 
protected void Button2_Click(object sender, EventArgs e) 
{ 
try 
{ 
string path = Server.MapPath("File/") + Session["txt"]; 
string path2 = Server.MapPath("File/") + "复制" + Session["txt"]; 
FileInfo fi = new FileInfo(path); 
if (fi.Exists) 
{ 
fi.CopyTo(path2);//将指定路径文件夹中的文件拷贝到该文件夹中,并将该文件改名 
} 
Label2.Text = "复制" + Session["txt"] + "成功!" + "文件为:" + ("复制" + Session["txt"].ToString()); 
List(); 
} 
catch (Exception error) 
{ 
Label2.Text = "复制文件出错,该文件已被复制过!"; 
} 
} 
protected void Button4_Click(object sender, EventArgs e) 
{ 
string path = Server.MapPath("File/") + ListBox1.SelectedValue.ToString(); 
string path2 = Server.MapPath("file2/") + ListBox1.SelectedValue.ToString(); 
FileInfo fi = new FileInfo(path); 
FileInfo fi2 = new FileInfo(path2); 
if (fi.Exists) 
{ 
if (!fi2.Exists) 
{ 
fi.MoveTo(path2);//将指定文件夹路径中的文件移动到另一个路径中的文件夹 
List(); 
} 
else 
{ 
Response.Write("<script language=javascript>alert('目标文件夹文件已经存在,不能移动改文件!');location='javascript:history.go(-1)'</script>"); 
} 
}
} 
protected void Button3_Click(object sender, EventArgs e) 
{ 
if (Session["txt"] == null) 
{ 
Label2.Text = "请选中文件后在执行删除操作!"; 
} 
FileInfo fi = new FileInfo(Server.MapPath("File/" + Session["txt"])); 
if (fi.Exists) 
{ 
fi.Delete();//删除文件 
Label2.Text = "删除" + Session["txt"] + "文件成功!"; 
List(); 
Session.Clear();//清空变量Session 
}
} 
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
Session["txt"] = ListBox1.SelectedValue.ToString(); 
} 
public void List()//获取指定文件夹文件名,并绑定ListBox控件显示在页面中 
{ 
DataTable dt = new DataTable(); 
dt.Columns.Add(new DataColumn("Name", typeof(string))); 
string serverPath = Server.MapPath("File"); 
DirectoryInfo dir = new DirectoryInfo(serverPath); 
foreach (FileInfo fileName in dir.GetFiles()) 
{ 
DataRow dr = dt.NewRow(); 
dr[0] = fileName; 
dt.Rows.Add(dr); 
} 
ListBox1.DataSource = dt; 
ListBox1.DataTextField = "Name"; 
ListBox1.DataValueField = "Name"; 
ListBox1.SelectedIndex = 0; 
ListBox1.DataBind(); 
}

您可能感兴趣的文章:
C#实现文件复制、移动与创建的实例代码
php ftp类(上传、下载、复制、移动等)
PHP移动文件或文件夹的方法浅析
根据文件名创建文件夹并将其移动进去的批处理脚本
创建目录结构的批处理脚本
JS文件读写代码 js读取与写入文件
php使用ftp下载文件的简单例子
php上传文件并创建递归目录的例子
mysql批量插入(insert)与批量更新(update)的例子
js创建、写入与读取文件实例详解

[关闭]
~ ~