c# - IO Exception was unhandled. The process cannot access the file because it is being used -
i wrote program, code seems working not working. gives io exception unhandled error. guys me , should delete because program try use same file @ same time. please me!!
namespace app1508 { public partial class form2 : form { string gooddir = "c:\\good\\"; string baddir = "c:\\bad\\"; string fromdir = "c:\\deneme\\"; list<image> images = null; int index = -1; fileinfo[] finfos = null; public form2() { initializecomponent(); directoryinfo di = new directoryinfo(@"c:\deneme"); finfos = di.getfiles("*.jpg",searchoption.topdirectoryonly); images = new list<image>(); foreach (fileinfo fi in finfos) { images.add(image.fromfile(fi.fullname)); } } private void button1_click(object sender, eventargs e) { finfos[index].moveto(path.combine("c:\\good", finfos[index].name)); } private void picturebox1_click(object sender, eventargs e) { index++; if (index < 0 || index >= images.count) index = 0; picturebox1.image = images[index]; } private void button2_click(object sender, eventargs e) { finfos[index].moveto(path.combine("c:\\bad", finfos[index].name)); } } }
this problem:
foreach (fileinfo fi in finfos) { images.add(image.fromfile(fi.fullname)); }
image.fromfile
open file handle, , won't close until dispose of image. you're trying move file without disposing of image has file open first.
i suspect if dispose of relevant image in button1_click
, button2_click
methods (being aware if it's showing in picturebox
you'll need remove there first) you'll find works.
Comments
Post a Comment