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中的文件资源释放,然后完成删除。

解决方案 »

  1.   

    FileStream fs = new FileStream("aa.gif", FileMode.Open);
                Image image = Image.FromStream(fs);
                this.pictureBox1.Image = image;
                fs.Close();
      

  2.   

    this.pictureBox1.Image.dispose();
    this.picturebox1.imgage = null;
    File.delete(strPath); //---FilePath
      

  3.   

    从读取图片那里讨论
    比如
    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);
      

  4.   

    this.pictureBox1.Image.dispose();
    this.picturebox1.imgage = null;
    File.delete(strPath); //---FilePath
    这样由时候也会出现 进程占用的情况 但是不知道怎么回事
    还是从源头上解决好
      

  5.   

    技术交流群号:23266021
    欢迎大家在此讨论关于.net的各种技术。1号群
      

  6.   

    pictureBox1.Image.Dispose();
     System.GC.Collect(); 再删除图片
      

  7.   

    this.pictureBox1.Image.dispose();
    this.picturebox1.imgage = null;
    System.GC.Collect();
    File.delete(strPath);