如何删除文件夹中的图片,文件。 file.delete() 里面必须传递的是绝对路径,有没有相对路径的。

解决方案 »

  1.   

    用这个插件吧,
    http://download.csdn.net/source/303617
    很方便
      

  2.   

    using System.IO;string strFilePath = Server.MapPath("..\\newspicture\\"+文件名);
    if(File.Exists(strFilePath))
    {
      File.Delete(strFilePath);//删除原来图片
    }
      

  3.   

    using System;
    using System.Data;
    using System.Configuration;
    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;/// <summary>
    /// DeleteFilePic 的摘要说明
    /// </summary>
    namespace Tools
    {
        public class DeleteFilePic
        {
            public static void SetListBoxItem(ListBox listBox, string sItemValue)
            {
                int index = 0;
                foreach (ListItem item in listBox.Items)
                {
                    ///判断值是否相等,并且设置控件的SelectedIndex
                    if (item.Value.ToLower() == sItemValue.ToLower())
                    {
                        listBox.SelectedIndex = index;
                        break;
                    }
                    index++;
                }
            }        public static void SetListBoxItem(DropDownList listBox, string sItemValue)
            {
                int index = 0;
                foreach (ListItem item in listBox.Items)
                {
                    ///判断值是否相等,并且设置控件的SelectedIndex
                    if (item.Value.ToLower() == sItemValue.ToLower())
                    {
                        listBox.SelectedIndex = index;
                        break;
                    }
                    index++;
                }
            }        /// 删除文件文件或图片
            /// </summary>
            /// <param name="path">当前文件的路径</param>
            /// <returns>是否删除成功</returns>
            public static void FilePicDelete(string path)
            {
                System.IO.FileInfo file = new System.IO.FileInfo(path);
                if (file.Exists)
                {
                    file.Delete();
                }
            }
        }
    }
      

  4.   

    string savePath = @"../UploadFiles/";
    if ((extension == ".jpg") | (extension == ".gif"))
                {
                    ImageName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + extension;
    savePath += ImageName;
    savePath3 = Server.MapPath(savePath3);
    //删除旧图片
                    DeleteFilePic.FilePicDelete(savePath3);
      

  5.   

    问什么非得要相对路径的,其实相对路径和绝对路径内部找的方式是一样的,只不过相对路径内部实现是在前面加上了驱动器的一串前缀,你可一用system.io.directory.getCurrentDirectory()或者Application.StartupPath获得当前工作路径(bin目录),然后加上图片的路径,很容易获得图片的引用,这样完全可以做到删除图功能。