c#文件的復(fù)制,移動,創(chuàng)建(實例代碼)
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();//創(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ù)制文件出錯,該文件已被復(fù)制過!";
}
}
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('目標(biāo)文件夾文件已經(jīng)存在,不能移動改文件!');location='javascript:history.go(-1)'</script>");
}
}
}
protected void Button3_Click(object sender, EventArgs e)
{
if (Session["txt"] == null)
{
Label2.Text = "請選中文件后在執(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控件顯示在頁面中
{
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();
}
相關(guān)文章
C# TcpClient網(wǎng)絡(luò)編程傳輸文件的示例
這篇文章主要介紹了C# TcpClient網(wǎng)絡(luò)編程傳輸文件的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-04-04WPF實現(xiàn)動畫效果(二)之From/To/By動畫
這篇文章介紹了WPF實現(xiàn)動畫效果之From/To/By動畫,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06C#中如何自定義配置上周和本周起始日來查詢業(yè)務(wù)數(shù)據(jù)(思路詳解)
在C#中并沒有封裝的方法根據(jù)我們需要來直接獲取上一周某天到某天、本周某天到某天,所以需要我們自己封裝方法來實現(xiàn)(我們也可以按照這個思路使用其他語言來實現(xiàn)),感興趣的朋友跟隨小編一起看看吧2023-09-09