国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

c#文件的復(fù)制,移動(dòng),創(chuàng)建(實(shí)例代碼)

2020-01-24 03:27:06
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

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('文件名錯(cuò)誤!');location='javascript:history.go(-1)'</script>");
    }
    else
    {
        try
        {
            string path = Server.MapPath("File") + "http://" + TextBox2.Text + DropDownList1.Text;
            FileInfo fi = new FileInfo(path);
            if (!fi.Exists)//如果文件不存在
            {
                fi.Create();//創(chuàng)建文件
                Label2.Text = "創(chuàng)建成功!文件名:" + 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/") + "復(fù)制" + Session["txt"];
        FileInfo fi = new FileInfo(path);
        if (fi.Exists)
        {
            fi.CopyTo(path2);//將指定路徑文件夾中的文件拷貝到該文件夾中,并將該文件改名
        }
        Label2.Text = "復(fù)制" + Session["txt"] + "成功!" + "文件為:" + ("復(fù)制" + Session["txt"].ToString());
        List();
    }
    catch (Exception error)
    {
        Label2.Text = "復(fù)制文件出錯(cuò),該文件已被復(fù)制過(guò)!";
    }
}
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);//將指定文件夾路徑中的文件移動(dòng)到另一個(gè)路徑中的文件夾
            List();
        }
        else
        {
            Response.Write("<script language=javascript>alert('目標(biāo)文件夾文件已經(jīng)存在,不能移動(dòng)改文件!');location='javascript:history.go(-1)'</script>");
        }
    }

}
protected void Button3_Click(object sender, EventArgs e)
{
    if (Session["txt"] == null)
    {
        Label2.Text = "請(qǐng)選中文件后在執(zhí)行刪除操作!";
    }
    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控件顯示在頁(yè)面中
{
    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();
}


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沙湾县| 广宗县| 贵德县| 延长县| 保德县| 乐亭县| 黄骅市| 汕尾市| 台南市| 佳木斯市| 贡觉县| 冷水江市| 阳泉市| 微山县| 葫芦岛市| 浠水县| 新安县| 伊川县| 大渡口区| 兴隆县| 山阴县| 锡林浩特市| 乌拉特后旗| 襄城县| 上饶县| 屏南县| 乐安县| 金寨县| 桐庐县| 鹰潭市| 西青区| 十堰市| 土默特右旗| 武胜县| 浙江省| 河西区| 阜宁县| 河西区| 伊宁市| 长汀县| 永宁县|