日期:2014-05-18  浏览次数:20470 次

asp.net 删除图片问题
我做了一个上传图片功能,把图片上传到了一个文件夹里,我又用DataGrid显示信息,然后用DataGrid的删除功能,但我只能删除图片在数据库中的路径字段,我要怎么才能在DataGrid中删除文件夹里的图片拉?
请给出详细代码!谢谢各位!我是用的是ACCESS数据库!ASP。NET(C#)

------解决方案--------------------
根据路径删图片啊。。
------解决方案--------------------
使用File.Delete()
------解决方案--------------------
C# code
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = (int)e.Keys["id"];
            int subjectid = int.Parse(DAL.dbHelper.ExecuteScalar("select top 1 pic from table1  where id=" + id.ToString()).ToString());
            string f = BLL.Bizhi.GetPic4(subjectid, id);
            if (File.Exists(f))
                File.Delete(f);
        }

------解决方案--------------------
if (System.IO.File.Exists(imgPath))//imgPath为你的图片路径
{
try
{
System.IO.File.Delete(imgPath);
}
catch (Exception)
{
Response.Write("无法删除!");
}
}
else
{
Response.Write("该Excel不存在!");
}

------解决方案--------------------
C# code

if(System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(Path)))
{
    System.IO.File.Delete(Path);
}

------解决方案--------------------
System.IO.File.Delete(Server.MapPath("~/"+imgPath));
即使图片不存在也不会出错的,所以可以省去判断图片是否存在的语句.