string x = filePath //文件路径   判断文件是否存在,存在则删除,求代码,谢谢了 
  
   

解决方案 »

  1.   

    if(System.IO.File.Exists(x))
    {
        System.IO.File.Delete(x);
    }
      

  2.   

    文件(图片)上传,上传之前先判断是否存在,如果文件已存在,则先删除。
    protected void btnUpfile2_ServerClick(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            mc.RunProc(sl.selAllProjectName(), ds, "ProjectInfo");
            string image = ds.Tables["ProjectInfo"].Rows[0]["projImage3"].ToString();
            if (!image.Equals(""))
            {
                //获取现有图片的路径
                string imgPath = Server.MapPath("proimage") + "\\" + image;
                //删除现有图片
                System.IO.File.Delete(imgPath);//删除照片
                HttpPostedFile hpf = this.file2.PostedFile;
                string FileSize = Convert.ToString(Convert.ToInt32(hpf.ContentLength.ToString()) / 1024) + "KB";
                //取得文件名(不含路径)
                char[] de ={ '\\' };
                string[] AFilename = hpf.FileName.Split(de);
                string strFilename = DateTime.Now.ToString().Replace("-", "").Replace(" ", "").Replace(":", "") + AFilename[AFilename.Length - 1];
                if (strFilename.Substring(strFilename.LastIndexOf('.') + 1) == "jpg" || strFilename.Substring(strFilename.LastIndexOf('.') + 1) == "gif" || strFilename.Substring(strFilename.LastIndexOf('.') + 1) == "JPG")
                {                hpf.SaveAs(Server.MapPath(".") + "\\proimage\\" + strFilename);
                    this.labFileName2.Text = strFilename.ToString();
                    ClientScript.RegisterStartupScript(this.GetType(), null, "<script>alert(\"图片上传成功!\");</script>");
                    return;            }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), null, "<script>alert(\"图片格式不正确!\");</script>");
                    return;
                }
            }
            else
            {
                HttpPostedFile hpf = this.file2.PostedFile;
                string FileSize = Convert.ToString(Convert.ToInt32(hpf.ContentLength.ToString()) / 1024) + "KB";
                //取得文件名(不含路径)
                char[] de ={ '\\' };
                string[] AFilename = hpf.FileName.Split(de);
                string strFilename = DateTime.Now.ToString().Replace("-", "").Replace(" ", "").Replace(":", "") + AFilename[AFilename.Length - 1];
                if (strFilename.Substring(strFilename.LastIndexOf('.') + 1) == "jpg" || strFilename.Substring(strFilename.LastIndexOf('.') + 1) == "gif" || strFilename.Substring(strFilename.LastIndexOf('.') + 1) == "JPG")
                {                hpf.SaveAs(Server.MapPath(".") + "\\proimage\\" + strFilename);
                    this.labFileName2.Text = strFilename.ToString();
                    ClientScript.RegisterStartupScript(this.GetType(), null, "<script>alert(\"图片上传成功!\");</script>");
                    return;            }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), null, "<script>alert(\"图片格式不正确!\");</script>");
                    return;
                }
            }
        }