listView列表出来的文件名保存在selection.SubItems[3].Text,1 pictureBox1.Image = Image.FromFile(selection.SubItems[3].Text);将这个图片文件显示在pictureBox1中,2 File.Delete(selection.SubItems[3].Text); 在listView中选中后删除这个文件,用到File.Delete,但是程序提示
The process cannot access the file 'acd.jpg' because it is being used by another process.请问如何可以把image中的文件资源释放,然后完成删除。
The process cannot access the file 'acd.jpg' because it is being used by another process.请问如何可以把image中的文件资源释放,然后完成删除。
Image image = Image.FromStream(fs);
this.pictureBox1.Image = image;
fs.Close();
this.picturebox1.imgage = null;
File.delete(strPath); //---FilePath
比如
this.PicturePath=Application.StartupPath+@"\"+FileName;
FileStream fs = new FileStream(this.PicturePath, FileMode.Open, FileAccess.Read);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
MemoryStream ms = new MemoryStream(bytes);
这样删除图片就没有进程占用了 this.pictureBox1.Image=Image.FromStream(ms);
this.picturebox1.imgage = null;
File.delete(strPath); //---FilePath
这样由时候也会出现 进程占用的情况 但是不知道怎么回事
还是从源头上解决好
欢迎大家在此讨论关于.net的各种技术。1号群
System.GC.Collect(); 再删除图片
this.picturebox1.imgage = null;
System.GC.Collect();
File.delete(strPath);