c#文件的复制,移动,创建(实例代码)

所属分类: 软件编程 / C#教程 阅读数: 120
收藏 0 赞 0 分享

复制代码 代码如下:

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# 大小写转换(金额)实例代码

C# 大小写转换(金额)实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm中Panel实现用鼠标操作滚动条的实例方法

由于在WinForm中Panel不能直接响应鼠标的滚动事件,只好采用捕获窗体的滚动事件。
收藏 0 赞 0 分享

C#访问应用程序配置文件的方法

C#访问应用程序配置文件的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#读写文件的方法汇总

C#读写文件的方法汇总,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#计算代码执行时间的方法

在一些测试工作时我们需要获得高精度的代码执行时间以比较其效率。
收藏 0 赞 0 分享

C# 动画窗体(AnimateWindow)的小例子

C# 动画窗体(AnimateWindow)的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# zxing二维码写入的实例代码

C# zxing二维码写入的实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

c#判断输入的是不是数字的小例子

c#判断输入的是不是数字的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

c# 深拷贝与浅拷贝的区别分析及实例

浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用. 深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的.
收藏 0 赞 0 分享

C#利用com操作excel释放进程的解决方法

最近利用Microsoft.Office.Interop.Excel.Application读取一个excel后,进程中一直存在excel,在网上找了一阵子,其中有几个解决方案
收藏 0 赞 0 分享
查看更多