0
votes

File delete problem - please help me i cant delete a file .. when i try to delete the i get this massage

c# File.Delete - File being used by another processc# File.Delete - File being used by another process

    protected void Button2_Click1(object sender, EventArgs e)
{

    HttpFileCollection hfc = Request.Files;
    string x = "";
    string foldername = DateTime.Now.ToString().Trim().Replace(" ", "").Replace(":", "").Replace("/", "");
    string foldername1 = foldername+"1";
    Directory.CreateDirectory(Server.MapPath("~/IMAGEUPLOADCENTER/") + foldername);
    Directory.CreateDirectory(Server.MapPath("~/IMAGEUPLOADCENTER/") + foldername1);
    for (int i = 0; i < hfc.Count; i++)
    {
        HttpPostedFile hpf = hfc[i];
        if (hpf.ContentLength > 0)
        {
            string name =(DateTime.Now.ToString() + i + hpf.FileName).ToString().Trim().Replace(" ", "").Replace(":", "").Replace("/", "");
            hpf.SaveAs(Server.MapPath("~/IMAGEUPLOADCENTER/" + foldername + "/") + name);
            ResizeImageWidth(497, Server.MapPath("~/IMAGEUPLOADCENTER/" + foldername + "/") + name, Server.MapPath("~/IMAGEUPLOADCENTER/" + foldername1 + "/") + name);
            System.Drawing.Image upImage = System.Drawing.Image.FromFile(Server.MapPath("~/IMAGEUPLOADCENTER/" + foldername1 + "/") + name);  //System.Drawing.Image.FromStream(FU1.PostedFile.InputStream);
            System.Drawing.Image logoImage = System.Drawing.Image.FromFile(Server.MapPath("~/pages002248Xc54/UploadImages/LOGOnew.png"));
            using (Graphics g = Graphics.FromImage(upImage))
            {
                g.DrawImage(logoImage, new Point(upImage.Width - logoImage.Width - 10, 10));
                upImage.Save(Server.MapPath("~/IMAGEUPLOADCENTER/" + foldername + "/") + name);
                File.Delete(Server.MapPath("~/IMAGEUPLOADCENTER/" + foldername1 + "/") + name);
             //   Image1.ImageUrl = "~/UploadFiles/2" + "//" + fileName;
            }
            x = x + "</br><img src='http://hela.co.il/IMAGEUPLOADCENTER/" + foldername + "/" + name + "'/></br>";
        }
    }
    TextArea1.InnerText = x;
    FileUpload1.Visible = false;
    Button2.Visible = false;
}
1
Why are you writing </br> instead of <br>? - Mr Lister
Please accept my answer if it was your problem. Or post a new answer if you found another solution. Thanks. - Caleb Mauer

1 Answers

3
votes

Your file is being locked by you and that is preventing you from deleting it. The Image.FromFile method locks the file used to create the object until the Image object is disposed of. So in this case, the image file will remain locked until you dispose of upImage.

Move your File.Delete to after you're done with the image and it will work. In the code below, I added using statements to each variable so that they will get disposed of (and unlock the file), and then I moved the delete statement outside of the using block (after the Image objects are disposed of).

using(System.Drawing.Image upImage =  System.Drawing.Image.FromFile(Server.MapPath("~/IMAGEUPLOADCENTER/" + foldername1 + "/") + name))     
using(System.Drawing.Image logoImage = System.Drawing.Image.FromFile(Server.MapPath("~/pages002248Xc54/UploadImages/LOGOnew.png")))
using (Graphics g = Graphics.FromImage(upImage))
{
    g.DrawImage(logoImage, new Point(upImage.Width - logoImage.Width - 10, 10));
    upImage.Save(Server.MapPath("~/IMAGEUPLOADCENTER/" + foldername + "/") + name);
}
File.Delete(Server.MapPath("~/IMAGEUPLOADCENTER/" + foldername1 + "/") + name);

Image.FromFile Method (String)

using statement (C#)