In the code below I receive an exception error because the target file is being used by another process. I know this to be false because the target doesn't exist. It seems like the application believes that the file is still in use by the application. My concern is that I do not think that I still have a reference to it. In the removeImage function, I remove the item from the image list and list view controls and refresh the list view control to ensure that it is no longer even on the screen. No joy.
Thanks in advance for any help, also if I have questions like this do i need to include the entire project or just the area of confusion?
Imports System.IO
Public Class Form1
Inherits System.Windows. Forms.Form
Dim mData As ArrayList
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows. Forms.Button
Friend WithEvents ImageList1 As System.Windows. Forms.ImageList
Friend WithEvents ListView1 As System.Windows. Forms.ListView
Friend WithEvents Button2 As System.Windows. Forms.Button
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
Me.components = New System.Componen tModel.Containe r
Me.Button1 = New System.Windows. Forms.Button
Me.ImageList1 = New System.Windows. Forms.ImageList (Me.components)
Me.ListView1 = New System.Windows. Forms.ListView
Me.Button2 = New System.Windows. Forms.Button
Me.SuspendLayou t()
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(224, 24)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing. Size(64, 48)
Me.Button1.TabI ndex = 0
Me.Button1.Text = "Button1"
'
'ImageList1
'
Me.ImageList1.I mageSize = New System.Drawing. Size(254, 130)
Me.ImageList1.T ransparentColor = System.Drawing. Color.Transpare nt
'
'ListView1
'
Me.ListView1.La rgeImageList = Me.ImageList1
Me.ListView1.Lo cation = New System.Drawing. Point(8, 16)
Me.ListView1.Na me = "ListView1"
Me.ListView1.Si ze = New System.Drawing. Size(208, 216)
Me.ListView1.Sm allImageList = Me.ImageList1
Me.ListView1.Ta bIndex = 1
'
'Button2
'
Me.Button2.Loca tion = New System.Drawing. Point(224, 80)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing. Size(64, 48)
Me.Button2.TabI ndex = 2
Me.Button2.Text = "Button2"
'
'Form1
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(292, 266)
Me.Controls.Add (Me.Button2)
Me.Controls.Add (Me.ListView1)
Me.Controls.Add (Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout (False)
End Sub
#End Region
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
Dim dirInfo As New DirectoryInfo(" c:\bcrcc\images ")
Dim tmpDir As DirectoryInfo
Dim fTemp() As FileInfo
Dim sGoodDirs As ArrayList
Dim excep As Exception
Dim bDone As Boolean = False
For Each tmpDir In dirInfo.GetDire ctories()
fTemp = tmpDir.GetFiles ("*.jpg")
If fTemp.GetUpperB ound(0) > 0 Then
RetrieveData(tm pDir.FullName, excep)
bDone = True
Exit For
End If
Next
End Sub
Private Sub RetrieveData(By Val sDirectory As String, ByVal e As System.Exceptio n)
Dim dir As Directory
Dim ddata() As String = dir.GetFiles(sD irectory, "*.jpg")
Dim iTemp As Integer
Dim iItem(ddata.Get UpperBound(0)) As ListViewItem
Dim sFile As String
Dim iLocation As Integer = 0
ImageList1.Imag es.Clear()
ListView1.Items .Clear()
mData = New ArrayList
For Each sFile In ddata
'ReDim Preserve mData(iLocation )
mData.Add(sFile )
ImageList1.Imag es.Add(System.D rawing.Image.Fr omFile(sFile))
iItem(iLocation ) = New ListViewItem(sF ile, iLocation)
ListView1.Items .Add(iItem(iLoc ation))
iLocation = iLocation + 1
Next
End Sub
Private Sub removeImage(ByV al sender As System.Object, ByVal e As System.EventArg s) Handles Button2.Click
ImageList1.Imag es.RemoveAt(0)
ListView1.Items .RemoveAt(0)
ListView1.Refre sh()
Dim ffile As File
Application.DoE vents()
ffile.Move(mDat a(0).ToString, mData(0).ToStri ng & ".tmp")
mData.RemoveAt( 0)
End Sub
End Class