c# - File.Delete being used by another process -
file delete problem - please me cant delete file .. when try delete massage
c# file.delete - file being used processc# file.delete - file being used 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); (int = 0; < hfc.count; i++) { httppostedfile hpf = hfc[i]; if (hpf.contentlength > 0) { string name =(datetime.now.tostring() + + 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; }
your file being locked , preventing deleting it. image.fromfile method locks file used create object until image object disposed of. in case, image file remain locked until dispose of upimage.
move file.delete after you're done image , work. in code below, added using statements each variable disposed of (and unlock file), , moved delete statement outside of using block (after image objects 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);
Comments
Post a Comment