S
Stephen Costanzo
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.
InitializeComponent()
'Add any initialization after the InitializeComponent() 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.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'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.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
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.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(224, 24)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(64, 48)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'ImageList1
'
Me.ImageList1.ImageSize = New System.Drawing.Size(254, 130)
Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
'
'ListView1
'
Me.ListView1.LargeImageList = Me.ImageList1
Me.ListView1.Location = New System.Drawing.Point(8, 16)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(208, 216)
Me.ListView1.SmallImageList = Me.ImageList1
Me.ListView1.TabIndex = 1
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(224, 80)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(64, 48)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Button2"
'
'Form1
'
Me.AutoScaleBaseSize = 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(ByVal sender As System.Object, ByVal e As System.EventArgs) 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.GetDirectories()
fTemp = tmpDir.GetFiles("*.jpg")
If fTemp.GetUpperBound(0) > 0 Then
RetrieveData(tmpDir.FullName, excep)
bDone = True
Exit For
End If
Next
End Sub
Private Sub RetrieveData(ByVal sDirectory As String, ByVal e As System.Exception)
Dim dir As Directory
Dim ddata() As String = dir.GetFiles(sDirectory, "*.jpg")
Dim iTemp As Integer
Dim iItem(ddata.GetUpperBound(0)) As ListViewItem
Dim sFile As String
Dim iLocation As Integer = 0
ImageList1.Images.Clear()
ListView1.Items.Clear()
mData = New ArrayList
For Each sFile In ddata
'ReDim Preserve mData(iLocation)
mData.Add(sFile)
ImageList1.Images.Add(System.Drawing.Image.FromFile(sFile))
iItem(iLocation) = New ListViewItem(sFile, iLocation)
ListView1.Items.Add(iItem(iLocation))
iLocation = iLocation + 1
Next
End Sub
Private Sub removeImage(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ImageList1.Images.RemoveAt(0)
ListView1.Items.RemoveAt(0)
ListView1.Refresh()
Dim ffile As File
Application.DoEvents()
ffile.Move(mData(0).ToString, mData(0).ToString & ".tmp")
mData.RemoveAt(0)
End Sub
End Class
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.
InitializeComponent()
'Add any initialization after the InitializeComponent() 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.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'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.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
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.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(224, 24)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(64, 48)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'ImageList1
'
Me.ImageList1.ImageSize = New System.Drawing.Size(254, 130)
Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
'
'ListView1
'
Me.ListView1.LargeImageList = Me.ImageList1
Me.ListView1.Location = New System.Drawing.Point(8, 16)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(208, 216)
Me.ListView1.SmallImageList = Me.ImageList1
Me.ListView1.TabIndex = 1
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(224, 80)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(64, 48)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Button2"
'
'Form1
'
Me.AutoScaleBaseSize = 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(ByVal sender As System.Object, ByVal e As System.EventArgs) 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.GetDirectories()
fTemp = tmpDir.GetFiles("*.jpg")
If fTemp.GetUpperBound(0) > 0 Then
RetrieveData(tmpDir.FullName, excep)
bDone = True
Exit For
End If
Next
End Sub
Private Sub RetrieveData(ByVal sDirectory As String, ByVal e As System.Exception)
Dim dir As Directory
Dim ddata() As String = dir.GetFiles(sDirectory, "*.jpg")
Dim iTemp As Integer
Dim iItem(ddata.GetUpperBound(0)) As ListViewItem
Dim sFile As String
Dim iLocation As Integer = 0
ImageList1.Images.Clear()
ListView1.Items.Clear()
mData = New ArrayList
For Each sFile In ddata
'ReDim Preserve mData(iLocation)
mData.Add(sFile)
ImageList1.Images.Add(System.Drawing.Image.FromFile(sFile))
iItem(iLocation) = New ListViewItem(sFile, iLocation)
ListView1.Items.Add(iItem(iLocation))
iLocation = iLocation + 1
Next
End Sub
Private Sub removeImage(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ImageList1.Images.RemoveAt(0)
ListView1.Items.RemoveAt(0)
ListView1.Refresh()
Dim ffile As File
Application.DoEvents()
ffile.Move(mData(0).ToString, mData(0).ToString & ".tmp")
mData.RemoveAt(0)
End Sub
End Class