Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have created an imagelist and listview programmatically and add a treeview in the win form. When the user will browse to an Image Root folder, it will create the listview and imagelist depending on the number of sub folder and also show the root folder and subfolders in the treeview. User will perform moving an image from the listview to the treeview. On my treeview_DragDrop event, when I call the File.Move() function, I get an error "The process cannot access the file because it is being used by another process". Which I understand the image in the imagelist need to be dispose and remove first. How can I dispose the object from the imagelist? Dispose and RemoveAt method are not working. here is the code snippet...
C#
private void treeView1_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(ListViewItem)))
            {
                string DropInPath = string.Empty;
                string SelectedImage = string.Empty;
                ListViewItem li = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
                TreeNode nodeToDropIn = this.treeView1.GetNodeAt(this.treeView1.PointToClient(new Point(e.X, e.Y)));
                if (nodeToDropIn == null) { return; }
                if (nodeToDropIn.Level > 0)
                {
                    try
                    {
                        DropInPath = nodeToDropIn.FullPath;
                        SelectedImage = SelectedFolder + "\\" + li.Text;
                        this.fldImageList.Images[li.Text].Dispose();
                        this.fldImageList.Images.RemoveAt(li.Index);
                        File.Move(SelectedImage, DropInPath);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

                if (li == null) { return; }
                nodeToDropIn.Nodes.Add(li.Text);
                fldListview.Items.Remove(li);
            }
Posted

1 solution

Actually, there's a simpler way to do this. Rather than keeping the original image, you need to keep a copy of the image instead. I demonstrate a low impact way to manage your images here[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900