2 lv139 lv139 于 2014.11.19 09:55 提问

C#中listview加载的图片,批量删除时出现异常“文件被另一个程序使用”,具体代码如下

自动加载图片:
private void FrmPicListView_Load(object sender, EventArgs e)
{
listView1.View = View.LargeIcon;
listView1.MultiSelect = true;
string[] files = GetImages();
if (files != null)
{
ImageList list = GetImage(files);
listView1.Tag = files;
if (listView1.LargeImageList == null)
{
listView1.LargeImageList = new ImageList();
listView1.LargeImageList.ImageSize = new Size(60, 80);
}
for (int i = 0; i < list.Images.Count; i++)
{
listView1.LargeImageList.Images.Add(list.Images.Keys[i], list.Images[i]);
list.Images[i].Dispose();
listView1.Tag = files[i];
}

            int count = listView1.Items.Count;
            if (files.Length == list.Images.Count)
            {
                for (int i = count; i < count + files.Length; i++)
                {
                    int index = i - count;
                    FileInfo info = new FileInfo(files[index]);

                    if (!listView1.Items.ContainsKey(files[index]))
                    {
                        listView1.Items.Add(files[index], info.Name, i);
                    }
                    else
                    {
                        MessageBox.Show("已存在此图片:" + files[index] + ",不能重复添加!");
                    }
                }
            }
        }
    }

    private ImageList GetImage(string[] files)
    {
        ImageList list = new ImageList();
        for (int i = 0; i < files.Length; i++)
        {
            Image image = Image.FromFile(files[i]);
            list.Images.Add(files[i], Image.FromFile(files[i]));
            list.ImageSize = new Size(60, 80);
        }
        return list;
    }
private int NumOfFiles;
    private string[] imgName;
    private string[] imgExtension;
    private string[] GetImages()
    {
        FileInfo FInfo;
        imgName = Directory.GetFiles(Application.StartupPath + @"\FlashPath\asset\photos");
        NumOfFiles = imgName.Length;
        imgExtension = new string[NumOfFiles];
        for (int i = 0; i < NumOfFiles; i++)
        {
            FInfo = new FileInfo(imgName[i]);
            imgExtension[i] = FInfo.Extension;
        }
        return imgName;
    }

批量删除代码:
private void butDelete_Click(object sender, EventArgs e)
{

        try
        {
            if (this.listView1.CheckedItems.Count > 0)
            {
                for (int i = 0; i < listView1.CheckedItems.Count;i++ )
                {
                    if (listView1.CheckedItems[i].Checked)
                    {
                        listView1.Items.Remove(listView1.CheckedItems[i]);
                        PathName = listView1.CheckedItems[i].Name;
                        File.Delete(PathName);
                    }
                }
            }
            else
            {
                MessageBox.Show("请先选择要删除的图片!", "信息提示");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("批量删除失败:" + ex.Message);
        }
    }
}

3个回答

yuwenge
yuwenge   2014.11.19 15:00

读取图片后未释放stream。

happy09li
happy09li   Ds   Rxr 2014.11.19 15:18

文件被占用,读取之后记得关闭流

caozhy
caozhy   Ds   Rxr 2014.11.20 10:35

先检查下是程序自身占用了文件还是外部程序占用了文件,如果是自身,看看读取文件后有没有及时关闭。

lv139
lv139 我是在一个winform界面中的listvie里先列表加载显示图片,然后通过点击删除按钮对图片进行删除操作,请问如何在加载图片完成以后进行关闭?
大约 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!