如何用File.Delete 方法批量删除文件?
如何用File.Delete 方法批量删除文件? --------------------编程问答-------------------- string strFilePhysicalPath = Server.MapPath(strFileUploadPath + sUrl);string strFilePhysicalPath2 = Server.MapPath(strFileUploadPath2 + sUrl);
比如说我想删除这两个文件.
File.Delete(strFilePhysicalPath);
File.Delete(strFilePhysicalPath2);
这么写?? --------------------编程问答-------------------- 如果同一个文件下的所有文件都删除,那么你直接可以删除整个文件夹. Directory.Delete
如果删除同一文件夹下某一中类型的所有文件,那File.
如果只删除同一文件夹下的几个单独的文件,(不是同一个类型的),那么就只有一个个来删除了 --------------------编程问答-------------------- using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class Admin_M_Admin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadGridView();
}
Delete.Attributes.Add("onclick", "return confirm('您确定要删除,这将不能恢复?')");
}
protected void LinkButton1_Click(object sender, EventArgs e)//实现全选择功能
{
if (((LinkButton)ExGridView1.HeaderRow.Cells[0].FindControl("LinkButton1")).Text == "全选")
{
for (int i = 0; i < ExGridView1.Rows.Count; i++)
{
((CheckBox)ExGridView1.Rows[i].Cells[0].FindControl("cb")).Checked = true;
}
((LinkButton)ExGridView1.HeaderRow.Cells[0].FindControl("LinkButton1")).Text = "清除";
}
else
{
for (int i = 0; i < ExGridView1.Rows.Count; i++)
{
((CheckBox)ExGridView1.Rows[i].Cells[0].FindControl("cb")).Checked = false;
}
((LinkButton)ExGridView1.HeaderRow.Cells[0].FindControl("LinkButton1")).Text = "全选";
}
}
protected void Delete_Click(object sender, EventArgs e)//删除功能
{
string sql = "delete from Users where";
string cal = "";
for (int i = 0; i < ExGridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)ExGridView1.Rows[i].Cells[0].FindControl("CB");
if (cb.Checked == true)
{
cal += " UserId=" + ExGridView1.DataKeys[i].Value.ToString() + " or";
}
}
if (cal != "")
{
sql += cal.Substring(0, cal.Length - 3);
}
else
{
sql = "";//不删除
}
//Response.Write(sql);//这里可以自己定义程序,进行删除任务
OleDbConnection conn = Connection.Dataconnection();
OleDbCommand Cmd = new OleDbCommand(sql, conn);
conn.Open();
Cmd.ExecuteNonQuery();
conn.Close();
Page.ClientScript.RegisterStartupScript(this.GetType(), "Startup", "<script>alert('您已经成功删除数据!');window.location.href='M_Admin.aspx'</script>");//无刷新弹出对话框
}
private void loadGridView()//实现数据绑定
{
OleDbConnection conn = Connection.Dataconnection();
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter("select * from Admin ORDER BY [A_DateTime] DESC", conn);
conn.Open();
da.Fill(ds, "Users");
conn.Close();
ExGridView1.DataKeyNames = new string[] { "UserId" };
ExGridView1.DataSource = ds.Tables["Users"];
ExGridView1.DataBind();
}
protected void ExGridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)//分页功能
{
ExGridView1.PageIndex = e.NewPageIndex;
ExGridView1.DataBind();
}
protected string MySubString(string Msub)
{
if (Msub.Length > 10)
{
return Msub.Substring(0, 10) + "...";
}
return Msub;
}
}
--------------------编程问答-------------------- 放到目录中 或路径放到哈希表中用foreach删除 --------------------编程问答-------------------- 只能用foreach了。
没有批量的这种方法。
--------------------编程问答-------------------- 只能用foreach了。
没有批量的这种方法。
--------------------编程问答-------------------- 恩,一个是Foreach
另外就是2楼所说的方法! --------------------编程问答-------------------- 如果要删除文件夹里面的全部文件,就删除文件夹,然后再建立。
这样代码量最少。 --------------------编程问答-------------------- 2樓 明白人,頂 --------------------编程问答-------------------- 可以调用古老的dos外部命令deltree.exe删除一切目录,包括非空目录 --------------------编程问答-------------------- 循环实现删除
补充:.NET技术 , ASP.NET