欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-17 19:29:40
复制代码 代码如下:protected void page_load(object sender, eventargs e) {     if...

复制代码 代码如下:

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();
}


上一篇:

下一篇: