1

I'm trying to make a file manager that can bring all files from the directory and subdirectories and list them all in a listView. I have completed everything except the Cut button.

this is my code

 private void Cut()
    {
        CutWasSelected = true;
        Clipboard.Clear();
        copiedFiles.Clear();
        pictureBox1.Image = null;
        WinMideaPlayer.URL = null;
        
        

        if (listView.SelectedItems.Count > 0)
        {

            foreach (ListViewItem item in listView.SelectedItems)

            {
                copiedFiles.Add(item.Name);
                if (listBothDirAndFiles.Contains(item.Name))
                {
                    listBothDirAndFiles.Remove(item.Name);
                }
                if (listfiles.Contains(item.Name))
                {
                    listfiles.Remove(item.Name);
                }
                if (listimages.Contains(item.Name))
                {
                    listimages.Remove(item.Name);
                }
                if (listvideo.Contains(item.Name))
                {
                    listvideo.Remove(item.Name);
                }
                if (listWordDocuments.Contains(item.Name))
                {
                    listWordDocuments.Remove(item.Name);
                }
                if (listPDFdocuments.Contains(item.Name))
                {
                    listPDFdocuments.Remove(item.Name);
                }
                if (listTxtFiles.Contains(item.Name))
                {
                    listTxtFiles.Remove(item.Name);
                }
                if (listCompressedFiles.Contains(item.Name))
                {
                    listCompressedFiles.Remove(item.Name);
                }
                
                item.Focused = false;
                item.Selected = false;
                listView.Items.Remove(item);
                
                

                byte[] moveEffect = new byte[] {2,0,0,0};
                MemoryStream dropEffect = new MemoryStream();
                dropEffect.Write(moveEffect, 0, moveEffect.Length);

                DataObject data = new DataObject();
                data.SetFileDropList(copiedFiles);
                data.SetData("Preferred DropEffect", dropEffect);
                

                Clipboard.Clear();
                Clipboard.SetDataObject(data);

                

            }
            

        }
       
    }

all files are working fine with this code including videos and text files except the images. when I select an Image and press on the cut button and then open the file explorer to paste the image I get a message says that " The action can't be completed because the file is open in TestExplorer4". I have searched my whole code and I'm pretty sure that I am not using the image anywhere in my project. So, could someone please explain to me why I am getting this message and how can I fix this problem. thank you in advance. enter image description here

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
AboSqr
  • 11
  • 1
  • If TestExplorer4 is your application and this code belongs to it, then your own application is holding onto the file so it cannot be moved. Make sure if any class is `IDisposable` and you have objects of that class to dispose of them. – CodingYoshi Jul 03 '20 at 01:47
  • thank you guys for your help, I found where the problem was. I was using SelectIndexChanged event to view the Images in PictureBox in my application and I was using image.fromFile method, this method was holding the image even when I delete it. Now, I replace that method with Image.FromStream and everything is working fine now. – AboSqr Jul 04 '20 at 02:24

1 Answers1

0

The file is in use by another program (probably VSCode or VisualStudio) and is locked. Here's how you can check: Is there a way to check if a file is in use? - to fix this first check if its locked, if it isn't no problem.

If it is locked then a make a temporary copy of the file and use that instead, then delete the temporary file copy.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321