Error while deleting a file


Aug 24, 2004
Programming Experience
I am displaying a list of images from a local directory using, listview and imagelist.
When the user selects an image(image1) and clicks a button i perform a rotation on that image and then save the rotated image(image1r) in the directory.
Now i want to delete the original image (i.e image1) from the directory, but when i try to do it.
i get this error

An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

Additional information: The process cannot access the file "C:\Documents and Settings\Komal\My Documents\My Pictures\friends\1005-003-84-1042.gif" because it is being used by another process.

I tried, disposing the imagelist, the listview, the bitmaps and graphics i am using to rotate the image. I even cleared the listview and imagelist and also set all the variables i am using to nothing but still i keep getting the same stupid error.
ANy ideas..?
a windows process is prolly useing the file which is why you're getting the error, make sure the system is done with the file first.

Still facing the problem...i really cant figure what st*p*d process is using the file....its so strange and frustrating

any help would be much appreciated
ya it is using the file and thats why i am clearing the imagelist and the listview and still i face this problem.
U said u faced this problem so how did u go about solving it?
When Michaelk says VS is using the image, he means Visual Studio (the program used to create the application). Is VS using the file?

Post your code if you still need a solution :)
Here is my code

My Code.... I get the error while deleting the file.....

Imports System.Drawing
Imports System.IO
Public Class Image3
Inherits System.Windows.Forms.Form

Dim listView1 As ListView = New ListView
Dim imageList1 As ImageList = New ImageList
Const IMAGE_DIRECTORY As String = "C:\Images\"
Dim files As String() = Directory.GetFiles(IMAGE_DIRECTORY)
Dim max_length,i As Integer
Dim s, fname As String
------------------------------------------------------------------- Private Sub Image3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.Controls.AddRange(New Control() {listView1})

End Sub
Private Sub ImageList_Load()
Dim pname As String
Dim myImage As Image
' Set the ColorDepth and ImageSize properties of imageList1.

imageList1.ColorDepth = ColorDepth.Depth8Bit
imageList1.ImageSize =
New System.Drawing.Size(70, 80)
max_length = files.Length - 2
' Add images to imageList1.

For i = 0 To max_length
pname = Path.GetFullPath(files(i))
myImage = Image.FromFile(pname)
myImage = Nothing
pname = Nothing
End Sub
Private Sub ListView_Load()

listView1.View = View.LargeIcon
listView1.LargeImageList = imageList1
listView1.Location =
New System.Drawing.Point(20, 40)
listView1.Name = "ListView1"
listView1.Size = New System.Drawing.Size(424, 264)
listView1.SmallImageList = imageList1

For i = 0 To files.Length - 2
fname = Path.GetFileName(files(i))
Dim listViewItem1 As ListViewItem = New ListViewItem(fname, i)
' Add listViewItem1 and listViewItem2.
listView1.Items.AddRange(New ListViewItem() _{listViewItem1})
listViewItem1 = Nothing
End Sub
Private Sub Button90_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim i, selected_index, wid, hgt As Integer
Dim f, Selected_fname, full_path, format, file_name, file_name_temp, directory_name, directory_name_temp, extension As String
Dim ArrTemp As New ArrayList
Dim arr As New ArrayList
Dim BlnStatus As Boolean
For i = 0 To listView1.SelectedIndices.Count - 1

selected_index = listView1.SelectedIndices.Item(i)
full_path = Path.GetFullPath(files(selected_index))
directory_name = Path.GetDirectoryName(files(selected_index))directory_name_temp = "C:\Temp"
Selected_fname = Path.GetFileName(files(selected_index))
file_name_temp = directory_name_temp & "\" & Selected_fname

' Make a Bitmap representing the input image.

Dim bm_in As New Bitmap(full_path)
wid = bm_in.Width
hgt = bm_in.Height
Dim bm_out As New Bitmap(wid, hgt, Imaging.PixelFormat.Format32bppRgb)Dim gr As Graphics = Graphics.FromImage(bm_out)
gr.DrawImage(bm_in, 0, 0, wid, hgt)
bm_out.Save(file_name_temp, Imaging.ImageFormat.Gif)
file_name = directory_name & "\" & Selected_fname

Me.Controls.AddRange(New Control() {listView1})
End Sub