c#文件的复制,移动,创建(实例代码)
时间:2023-05-29 21:49:14
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();
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java并发编程_线程池的使用方法(详解)
![](https://img.aspxhome.com/file/2023/1/69711_0s.png)
C# 判断字符串第一位是否为数字
springboot2.x使用Jsoup防XSS攻击的实现
![](https://img.aspxhome.com/file/2023/3/58733_0s.png)
C#中HttpWebRequest的用法详解
新手Hadoop安装 环境搭建
![](https://img.aspxhome.com/file/2023/2/74982_0s.jpg)
Java算法实现调整数组顺序使奇数位于偶数之前的讲解
C++异常处理入门(try和catch)
安卓(Android)动态创建多个按钮并添加监听事件
java8新特性 stream流的方式遍历集合和数组操作
![](https://img.aspxhome.com/file/2023/0/60790_0s.jpg)
springboot项目如何设置session的过期时间
![](https://img.aspxhome.com/file/2023/9/102649_0s.png)
Android实现九宫格拼图游戏
![](https://img.aspxhome.com/file/2023/4/95964_0s.png)
java二维数组遍历的2种代码
![](https://img.aspxhome.com/file/2023/7/63997_0s.png)
实例详解SpringBoot默认的JSON解析方案
maven手动上传jar包示例及图文步骤过程
![](https://img.aspxhome.com/file/2023/1/59571_0s.png)
springboot+jwt+微信小程序授权登录获取token的方法实例
![](https://img.aspxhome.com/file/2023/5/92725_0s.jpg)
Java+Swing实现五子棋游戏的示例代码
![](https://img.aspxhome.com/file/2023/9/66079_0s.jpg)
Java基于命令模式实现邮局发信功能详解
![](https://img.aspxhome.com/file/2023/3/69353_0s.jpg)
在Parallel中使用DbSet.Add()发现的一系列多线程问题和解决思路详解
![](https://img.aspxhome.com/file/2023/2/129642_0s.png)
详细解读JAVA多线程实现的三种方式
Unity快速生成常用文件夹的方法
![](https://img.aspxhome.com/file/2023/1/83241_0s.jpg)