Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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?

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

Edited by PlausiblyDamp
Posted
Could you only show revelant code or put them in [ code ] [ /code ] tag (without spaces) ? It would ease our reading.

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

Posted

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.

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...