c#檔案的複製,移動,建立(執行個體代碼)

來源:互聯網
上載者:User

複製代碼 代碼如下: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();
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.