先来说一下背景,软件有一个导入jpg照片的功能,当将照片导入后,利用缩略图生成函数,生成原始照片的缩略图,之后将缩略图加载到imagelist中,在listview的largeicon属性,绑定imagelist,来显示缩略图。
    问题出现在,关闭软件前,在FormClosed事件里写入了清除各种资源的代码,其中首先清除了imagelist中的image资源,清楚了listview中的items资源。这之后,在用for语句清除缩略图文件夹下的缩略图时,报出错误:“文件“C:\RoverLocatorDataBackup\thumbnail\_MG_0432.JPG”正由另一进程使用,因此该进程无法访问该文件。”   和该错误相关的代码如下:
(1) 导入像片的部分函数
 //把数据加载到列表中
                    for (int i = 0; i < tempphotoFiles.Length; i++)
                    {
                        //将每次打开的相片地址添加到动态数组alPhotoFiles
                        alPhotoFiles.Add(tempphotoFiles[i]);                        //progressBar1运行
                        this.toolStripProgressBar1.PerformStep();                        //调用GenThumbnail函数,生成缩略图
                        GenThumbnail(tempphotoFiles[i], thumbnailFilesPath + tempphotoFiles[i].Substring(tempphotoFiles[i].LastIndexOf(@"\") + 1), 80, 64);                        //向photonode中添加子节点,修改选定子节点的图片,展开所有子节点                      
                        this.PhotoNode.Nodes.Add(tempphotoFiles[i].Substring(tempphotoFiles[i].LastIndexOf(@"\") + 1), tempphotoFiles[i].Substring(tempphotoFiles[i].LastIndexOf(@"\") + 1), 2, 2);                    }         //向imagelist中添加缩略图项
                    for (int i = 0; i < Thumbnailfileinfo.Length; i++)
                    {
                        if (photoImage != null)
                        {
                            photoImage = null;
                        }                        photoImage = Image.FromFile(Thumbnailfileinfo[i]);
                        this.imageList1.Images.Add(photoImage);
                        //向缩略图窗口中添加相片名标识
                        this.listView1.Items.Add(Thumbnailfileinfo[i].Substring(Thumbnailfileinfo[i].LastIndexOf(@"\") + 1), i);
                    }                    PhotoNode.ExpandAll();                    //将动态数组转换添加到photoFiles
                    photoFiles = (string[])alPhotoFiles.ToArray(typeof(string));                    this.toolStripProgressBar1.Visible = false;                    photoImage = null;(2) 软件关闭前,清除各种资源的函数
 clearPhotoAndImage();            //清除treeview1中的子节点
            this.TxtNode.Nodes.Clear();
            this.PhotoNode.Nodes.Clear();
            this.ImageNode.Nodes.Clear();
            this.panoNode.Nodes.Clear();            //清除listView中的子节点
            this.listView1.Items.Clear();       
            this.listView2.Items.Clear();            //清除imageList中的子节点
            this.imageList1.Images.Clear();
      
            txtFiles = null;
            tempsourceTifFileName = null;            photoFiles = null;
            tempphotoFiles = null;
            Thumbnailfileinfo = null;            sourceTifFileName = null;
            tempsourceTifFileName = null;            panonum = 0;            alTXTFiles.Clear();
            alsourceTifFileName.Clear();
            alPhotoFiles.Clear();
            alpanofile.Clear();            Form.xColumn = null;
            Form.yColumn = null;
            Form.zColumn = null;
            Form.jiao1Column = null;
            Form.jiao2Column = null;
            Form.jiao3Column = null;            panoLocateForm.panoname.Clear();            panolocateresultForm.dt.Clear();
            panolocateresultForm.lstName.Clear();
            panolocateresultForm.lstX.Clear();
            panolocateresultForm.lstY.Clear();            dataResultForm.lstName.Clear();
            dataResultForm.lstX.Clear();
            dataResultForm.lstY.Clear();
            dataResultForm.lstZ.Clear();
            dataResultForm.lst1.Clear();
            dataResultForm.lst2.Clear();
            dataResultForm.lst3.Clear();
            System.IO.File.Delete(@"C:\RoverLocatorDataBackup\GCPPhotoData.txt");            //删除缩略图文件夹所有内容
            if (Directory.Exists(thumbnailFilesPath))
            {
                string[] thumbfile = System.IO.Directory.GetFiles(thumbnailFilesPath);                //这里为操作文件代码
                for (int i = 0; i < thumbfile.Length; i++)
                {
                    System.IO.File.Delete(thumbfile[i]);                }

            }问题就出在红色字体标识的部分,望各位高手指点,谢谢!