Cannot delete a file

hog

Well-known member
Joined
Mar 17, 2003
Messages
984
Location
UK
I am using the following to delete a file which is being displayed in a picturebox.

Code:
File.Delete(strFileName)

I keep getting an error message saying the file is in use by another process and cannot be deleted?

I have set the picturebox to nothing, I have set the bitmap used to fill the picturebox to nothing and even set the FileInfo() object which holds the file name to nothing and still get the error????

Where am I going wrong?

Thnx
 
if the file a picture(since you said that you are displaying it in a picture box)
and if so(stupid question) is it open by another app or in a share maybe?
or... are you using winXp? I know it really have problem letting go of past used things...
theye should be a way to destroy any link the OS have with any files... but M$ dont want that... I wouldnt care killing windows a couple of time if I could kill every process and delete any file I want when I want...
 
Yeah I am running WIndozeXP :-\

I have tried this so far and still no joy, and my program IS the only program opening the bitmap to view?

Code:
m_bmpNewImage.Dispose()

fiFileInfo = Nothing

Me.picMain.Image = Nothing

File.Delete(strFileName)
 
A reference to that file might still be in memory. You could force a Garbage collection and see if that removes the file fomr memory.

gc.collect
 
hog said:
Yeah I am running WIndozeXP :-\

I have tried this so far and still no joy, and my program IS the only program opening the bitmap to view?

Code:
m_bmpNewImage.Dispose()

fiFileInfo = Nothing

Me.picMain.Image = Nothing

File.Delete(strFileName)


No offence PlausiblyDamp but it seems to me hog tried to dispose of the bitmap as you sugested, but with no joy.

If it wasnt for this I wouldnt has suggested the garbage collection. I realise that windows does it own garbage collection in it own time but sometimes you have to give it a push.
 
If not the image then the FileStream used to load the image may need disposing, although without seeing the loading code that could be a bit of guesswork.
 
This is all the code that has anything to do with the file.

bmpResized = New Bitmap(CInt(bmpTooBig.Width * decRatio), CInt(bmpTooBig.Height * decRatio))

grpBitmap = Graphics.FromImage(bmpResized)

fiFileInfo = diDirectory.GetFiles("*.jpg")

m_bmpNewImage = ResizeBitmap(New Bitmap(strFileName), intPictureBoxWidth, intPictureBoxHeight)

me.picMain.image = m_bmpNewImage

I have tried setting all to nothing and disposing and nothing works except the gc.collect!
 
Here are the sections of code that have anything to do with the file.

Code:
Private Function ResizeBitmap(ByVal bmpTooBig As Bitmap, ByVal maxWidth As Integer, ByVal maxHeight As Integer) As Bitmap

        Dim decRatio, decRatioW, decRatioH As Decimal
        Dim bmpResized As Bitmap
        Dim grpBitmap As Graphics

        If bmpTooBig.Width > maxWidth Then

            decRatioW = maxWidth / bmpTooBig.Width

        Else

            decRatioW = 1

        End If

        If bmpTooBig.Height > maxHeight Then

            decRatioH = maxHeight / bmpTooBig.Height

        Else

            decRatioH = 1

        End If

        determine which dimension is the smallest

        If decRatioW <= decRatioH Then

            decRatio = decRatioW

        Else

            decRatio = decRatioH

        End If

        bmpResized = New Bitmap(CInt(bmpTooBig.Width * decRatio), CInt(bmpTooBig.Height * decRatio))

        Make a Graphics object for the resized bitmap.

        grpBitmap = Graphics.FromImage(bmpResized)

        grpBitmap.DrawImage(bmpTooBig, 0, 0, bmpResized.Width + 1, bmpResized.Height + 1)

        If Me.chkFullScreen.Checked Then

            Me.picMain.Location = New Point((intSavePictureBoxWidth - bmpResized.Width) / 2, 0)

        Else

            Me.picMain.Location = New Point((intSavePictureBoxWidth - bmpResized.Width) / 2, 104)

        End If

        Me.picMain.Width = bmpResized.Width

        Return bmpResized

        dispose of graphic objects

        grpBitmap.Dispose()

    End Function

Code:
Private Sub LoadSites()

        Try
             start up path to find photos

            Dim strPath As String = gstrPhotosFolderPath & Me.cboSites.SelectedItem & "\" & Me.cboDates.SelectedItem

            Dim diDirectory As New DirectoryInfo(strPath)

            Dim fiTemp As FileInfo

             create an array of the files in the current directory

            fiFileInfo = diDirectory.GetFiles("*.jpg")

            intPhotoCount = fiFileInfo.Length

            intIndex = 0

             store the first photo path

            strFileName = gstrPhotosFolderPath & Me.cboSites.SelectedItem & "\" & Me.cboDates.SelectedItem & "\" & fiFileInfo(intIndex).ToString

             load the image

            m_bmpNewImage = ResizeBitmap(New Bitmap(strFileName), intPictureBoxWidth, intPictureBoxHeight)

             display it

            Me.picMain.Image = m_bmpNewImage

        Catch objException As Exception

            MessageBox.Show(objException.Message)

        End Try

    End Sub
 
in the line
Code:
m_bmpNewImage = ResizeBitmap(New Bitmap(strFileName), intPictureBoxWidth, intPictureBoxHeight)

the New Bitmap(strFileName) part is probably locking the file. Try creating a temp bitmap, passing that in and disposing of the temp one to see if the problem goes away.
 

Similar threads

T
Replies
0
Views
574
This_display_name_is_already_in_use_They_all_are
T
Back
Top