End Process

lothos12345

Well-known member
Joined
May 2, 2002
Messages
294
Location
Texas
I am having trouble deleting a file. It keeps telling me that the file is in use by another process. I have pasted my code below, please any help would greatly be appreciated. What is keeping that file locked?
Code:
 Dim selections() As String
    Dim selection As String
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim regkey As Microsoft.Win32.RegistryKey
        Dim keypath As String = "Software\VB and VBA Program Settings\storename\verify"
        Dim frmreg As New Form2()
        Try
            regkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(keypath, False)
            If regkey Is Nothing Then
                Beep()
                Me.Enabled = False
                frmreg.Show()
                Exit Sub
            End If
            regvalue = CStr(regkey.GetValue("storekey", ""))
        Catch ex As Exception
            MessageBox.Show(Err.Description, "Call Client Support", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End
        End Try

        Dim x As Integer
        Try
            selections = Directory.GetFiles("c:\test2\")
            selections = Directory.GetFiles("E:\DCIM\100_0101\")
            For Each selection In selections
                ListBox1.Items.Add(selections(x))
                selections(x) = Nothing
                x += 1
            Next
            x = 0
        Catch ex As Exception
            MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Try
            pbimage.Image = System.Drawing.Image.FromFile(ListBox1.SelectedItem)
        Catch ex As Exception
            ListBox2.SetSelected(ListBox2.Items.Count - 1, True)
            pbimage.Image = System.Drawing.Image.FromFile(ListBox2.SelectedItem)
        End Try
    End Sub

    Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
        Try
            pbimage.Image = Nothing
            ListBox2.Items.Add(ListBox1.SelectedItem)
            ListBox2.SetSelected(ListBox2.Items.Count - 1, True)
            pbimage.Image = System.Drawing.Image.FromFile(ListBox2.SelectedItem)
            ListBox1.Items.Remove(ListBox1.SelectedItem)
        Catch ex As Exception
            MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btntransfer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntransfer.Click
        Dim rename As String
        Dim x As String
        x = txtvin.Text
        If x.Length <> 17 Then
            MessageBox.Show("You have typed the vin number incorrectly", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        If ListBox2.Items.Count > 0 Then
            Dim i As Integer
            For i = 0 To ListBox2.Items.Count - 1
                selections(i) = (ListBox2.Items(i))
            Next
        End If

        Try
            For Each selection In selections
                Dim y As Integer
                If selection = Nothing Then
                    Exit For
                End If
                rename = selection.Substring(17, selection.Length - 17)
                rename = txtvin.Text & "-" & y & ".jpg"
                File.Copy(selection, "c:\test\" & regvalue & "\" & rename, True)
                y += 1
            Next

        Catch ex As Exception
            MessageBox.Show(Err.Description)
        End Try
        pbimage.Image.Dispose()
        pbimage.Image = Nothing
        pbimage.Refresh()

*------Here is were the error occurs------------*
        For Each selection In selections
            If selection = Nothing Then
                Exit For
            End If
            File.Delete(selection)
        Next
*----------------------------------------------*
    End Sub

    Private Sub ListBox2_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox2.DoubleClick
        Try
            pbimage.Image = Nothing
            ListBox1.Items.Add(ListBox2.SelectedItem)
            ListBox1.SetSelected(ListBox1.Items.Count - 1, True)
            pbimage.Image = System.Drawing.Image.FromFile(ListBox2.SelectedItem)
            ListBox2.Items.Remove(ListBox2.SelectedItem)
        Catch ex As Exception
            MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        pbimage.Image.Dispose()
        Dim i As Integer
        For i = 0 To ListBox1.SelectedItems.Count - 1
            ListBox2.Items.Add(ListBox1.SelectedItems(i))
            selections(i) = ListBox1.SelectedItems(i)
        Next
        For i = 0 To ListBox1.SelectedItems.Count - 1
            ListBox1.Items.Remove(selections(i))
            selections(i) = Nothing
        Next
    End Sub

    Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
        pbimage.Image.Dispose()
        Dim i As Integer
        For i = 0 To ListBox2.SelectedItems.Count - 1
            ListBox1.Items.Add(ListBox2.SelectedItems(i))
            selections(i) = ListBox2.SelectedItems(i)
        Next
        For i = 0 To ListBox2.SelectedItems.Count - 1
            ListBox2.Items.Remove(selections(i))
            selections(i) = Nothing
        Next
    End Sub
End Class
 
Last edited by a moderator:
Which line(s) are generating the error? Is this the only application that should be accessing the file or is it possible another application is?
 
I briefly reviewed your code and may have missed something. . .

it appears you are creating a list of file names -selection, copying the files whose name correspond to the list into a different directory, then deleting
the files listed in selection.

have you tried to simply use the move method instead of copy/delete? It is much more efficient.

Why the copy/delete locks, I dont know.
If the move works, then it is a side effect of the copy/delete. If move doesnt work then start looking at other processes. thats what I would do.
 

Similar threads

Back
Top