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();
}
猜你喜欢
- 1、通过查找API文档:2、Map.Entry是一个接口,所以不能直接实例化。3、Map.entrySet( )返回的是一个collecti
- 此篇文章内容仅限于 描述springboot与 thy 自定义标签的说明,所以你在看之前,请先会使用springboot和thymeleaf
- 一、redis发布订阅简介Redis发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收信息。可以参
- 一、synchronized 有不足新事物的出现要不是替代老事物,要么就是对老事物的补充JUC 的 locks 就是对 synchroniz
- 本文实例讲述了Java正则验证正整数的方法。分享给大家供大家参考,具体如下:package des;import java.util.reg
- 想必我们在做项目的时候,都会遇到服务端与客户端交互数据。一般情况下我们都会采用json格式或者xml格式,将服务端的数据转换成这两种格式之一
- 网页爬虫:其实就是一个程序用于在互联网中获取符合指定规则的数据。package day05; import java.io.Buffered
- 一、业务背景有些业务请求,属于耗时操作,需要加锁,防止后续的并发操作,同时对数据库的数据进行操作,需要避免对之前的业务造成影响。二、分析流程
- Java 中的 CyclicBarrier 是一种同步工具,它可以让多个线程在一个屏障处等待,直到所有线程都到达该屏障处后,才能继续执行。C
- 前言:本文源码基于spring-framework-5.3.10。mvc是spring源码中的一个子模块!一、RequestMappingH
- 一、理解 “ 服务器 / 浏览器 ”沟通流程(3步)第1步:浏览器使用<img src=&qu
- 一. 思路今天接到个小任务,让把json文件转换成excel文件,按照列展开.思路:既然json已经都已经是现成的,那直接将jso
- 在本文中,笔者向大家介绍下Java中一个非常重要也非常有趣的特性,就是自动装箱与拆箱,并从源码中解读自动装箱与拆箱的原理,同时这种特性也留有
- 继承的概念继承是面向对象编程中的一个概念,它允许一个类(称为子类或派生类)继承另一个类(称为父类或基类)的属性和方法。子类在继承父类的同时也
- 本文实例为大家分享了java实现简易飞机大战的具体代码,供大家参考,具体内容如下整体思路1.创建游戏窗体,添加面板JPanel,重写JPan
- 1. 算法分析根据概率将奖品划分区间,每个区间代表一个奖品,然后抽取 随机数,反查落在那个区间上,即为所抽取的奖品。2. 代码核心
- 一、数组创建1.1 声明并赋值int[] a = {1,2,3};1.2 声明数组名开辟空间并且赋值int[] a;a = new int[
- 本文为大家分享了Android实现带动画效果的可点击展开TextView 制作代码,效果图: 收起(默认)效果:点击展开后的效果:源码: 布
- 本文实例为大家分享了java连接SQL Server数据库的具体代码,供大家参考,具体内容如下操作系统:windows 10 64位java
- 简单的理解,MyBatis逆向工程,就是通过相应插件,自动生成MyBatis数据库连接的一些文件。mybatis需要编写sql语句,myba