c# - Cannot delete folder because it is being used by another > process (my app) -
c# - Cannot delete folder because it is being used by another > process (my app) -
this question has reply here:
delete file beingness used process 3 answersmy wpf app creates temp directory (@"c:\myapptemp\"). within directory there downloaded images. after point in app (background workercompleted) dont need more folder , correspoding files, want deleted folder, tried
if (directory.exists(@"c:\myapptemp\")) { iofileutils.deletedirectory(@"c:\myapptemp\", true); if (!directory.exists(@"c:\myapptemp\")) { displaymessage = "temp files deleted"; } }
but i'm getting expcetion {"the process cannot access file 'c:\myapptemp\client1\6.jpg' because beingness used process."}
iofileutils.cs public static void deletedirectory(string path, bool recursive) { // delete files , sub-folders? if (recursive) { // yep... let's var subfolders = directory.getdirectories(path); foreach (var s in subfolders) { deletedirectory(s, recursive); } } // files of folder var files = directory.getfiles(path); foreach (var f in files) { // attributes of file var attr = file.getattributes(f); // file marked 'read-only'? if ((attr & fileattributes.readonly) == fileattributes.readonly) { // yes... remove 'read-only' attribute, file.setattributes(f, attr ^ fileattributes.readonly); } // delete file, raises exception!! file.delete(f); } // when here, files of folder // deleted, delete empty folder directory.delete(path); }
update code below raises exception
var photosontempdir = directory.getfiles(dirname); int imgcounter = 0; //used create file name system.drawing.image loadedimage; foreach (var image in photosontempdir) { loadedimage = system.drawing.image.fromfile(image); imageext = path.getextension(image); imgcounter++; var convertedimage = helpers.imagehelper.imagetobytearray(loadedimage); var img = new myimage { imagefile = convertedimage, name = imgcounter.tostring() }; myobj.images.add(img); }
make sure utilize "using" involves files. hold kind of handle not disposed yet 1 of files, - preventing deleting it.
c# .net wpf
Comments
Post a Comment