Close file used by visual basic . net

Asked

Viewed 259 times

1

Guys I’m using the following code to grab images from a directory and put in a Listview:

    For Each arquivos In FileIO.FileSystem.GetFiles(caminho, FileIO.SearchOption.SearchAllSubDirectories, "*.png")
        Dim nome_arq As String = System.IO.Path.GetFileNameWithoutExtension(arquivos)
        imagem.Images.Add(Image.FromFile(arquivos))
        ListView1.LargeImageList = imagem
        ListView1.Items.Add(nome_arq, contador)

And I wanted to use the command below to delete the image that was open in Listview1

     My.Computer.FileSystem.DeleteFile(dicionario(posicao), 
                                        FileIO.UIOption.OnlyErrorDialogs,
                                        FileIO.RecycleOption.SendToRecycleBin,
                                        FileIO.UICancelOption.DoNothing)

The problem is that I get error message saying that the file is already open and so cannot be closed.

So my question is how to remove the image so I can delete it?

1 answer

1


The Image.Fromfile Method keeps the file open. You should do it another way. You can and greatly improve your code, but to solve your problem, I did as below (based in that answer) and worked well:

Imports System.IO

Public Class Form1
    Private caminho As String = "C:\teste"

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim xx As Image
        Dim contador As Integer = 0
        ListView1.Clear()
        imagem.Images.Clear()
        For Each arquivos In FileIO.FileSystem.GetFiles(caminho, FileIO.SearchOption.SearchAllSubDirectories, "*.png")

            Using str As Stream = File.OpenRead(arquivos)
                xx = Image.FromStream(str)
                imagem.Images.Add(xx)
            End Using

            ListView1.LargeImageList = imagem
            ListView1.Items.Add(arquivos, contador)
            contador += 1
        Next
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        My.Computer.FileSystem.DeleteFile(ListView1.SelectedItems(0).Text,
                                        FileIO.UIOption.OnlyErrorDialogs,
                                        FileIO.RecycleOption.SendToRecycleBin,
                                        FileIO.UICancelOption.DoNothing)
        ListView1.Clear()
        Button1_Click(Nothing, Nothing)
    End Sub

End Class
  • It was bad for the code starting now, but this code is with a problem for min. The name of the images change but the images remain the same, I made a test with 3 images and the name changed but they were with the same "cover".

  • I edited the answer. The counter was inside the looping

  • When I delete for example the first image, it is deleted from pc, but not from listview. The name corresponding to the image is gone, but a different image is deleted from listview.

  • I edited the answer, you need to clear your image and listview.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.