本人菜鸟,用vs2008做了个企业网站,有上传图片的功能,放到服务器的指定文件夹里,在管理页面可以删除图片在数据库里的信息,怎么样才能把服务器上的图片也一起删除掉,最好有个代码 看看高手 帮忙啊

解决方案 »

  1.   

     调用删除图片类 DeletePhoto(Request.MapPath(photoName).ToString());//删除文件夹的图片,只读先改为可读写,然后再删除
    private void DeletePhoto(string photoPath)
        {
            FileInfo fi = new FileInfo(photoPath);
            if (fi.Exists)
            {
                if (fi.Attributes.ToString().ToLower().IndexOf("readonly") >= 0)
                {
                    fi.Attributes = FileAttributes.Normal;
                }
                try
                {
                    fi.Delete();
                }
                catch
                {            }
            }
        }
      

  2.   


      /// <summary>
         /// 删除文件
         /// </summary>
         /// <param name="filePath">文件绝对路径</param>
         public void DeleFile(string filePath)
         {
             try
             {
                 System.IO.FileInfo DeleFile = new System.IO.FileInfo(filePath);
                 if (DeleFile.Exists)
                 {
                     DeleFile.Delete();
                 }
             }
             catch (Exception ex)
             {
                 throw ex;
             }
         }
      

  3.   

    怎么把这个功能加在这个代码里呢protected void BtnDelete_Command(object sender, CommandEventArgs e)
        {
            OleDbConnection MyConn = DB.CreateDB();
            int id = int.Parse(e.CommandArgument.ToString());
            MyConn.Open();
            OleDbCommand cmd = new OleDbCommand("delete from cgal where id=" + id, MyConn);
            cmd.ExecuteNonQuery();
            MyConn.Close();
            this.DataToBind();
        }
      

  4.   

    找到图片在服务器的路径,然后File.Delete(path+filename)//删除指定路径的图片
      

  5.   

    用Server.MapPath()获取文件绝对路径名,
    用File.Delete()不就删除了。
    如:
    System.IO.File.Delete(Server.MapPath(@"../flash/images/") + GridView1.Rows[e.RowIndex].Cells[4].Text);
      

  6.   

    用Server.MapPath()获取文件绝对路径名,
    用File.Delete()不就删除了。
    如:
    System.IO.File.Delete(Server.MapPath(@"../flash/images/") + GridView1.Rows[e.RowIndex].Cells[4].Text);----------------
    可以实现,最好加上try catch判断。不过要注意了,访问用户要有操作的权限。需要注意修改权限哦!不然不可以删除成功的哦
      

  7.   

    我在数据库里有个字段保存图片 的路径,怎么根据ID把路径取出来呢,字段名是:imgurl 路径格式:imgurl
    images/com/20104008094056.jpg