locked
How To Properly Delete A File Using File.Delete() RRS feed

  • Question

  • User1177197882 posted

    Hello:

    My Code below Writes a file to the directory _photo/tempstuff from an Upload Control ( upload_Art )
    It then resizes the file, and then is suppose to delete the file

    Well It is not deleting the file.  Even when I go into Filezilla (my ftp client), and try to delete the file from my server,
    I get a message " 550 The process cannot access the file because it is being used by another process.  "

    So is there something I have to do to release the file after it is created so that it can be delete?
    Or is there something I have to do after the command that is suppose to delete the file ( File.Delete(imgFileName ) to release it
    so that it can actually be deleted?

    Please help, for as you can imagine, If I can't delete temp files from my server, I will run out of storage space!

    Thanks!!!

    IIS 7.0 and ASP 4.0 on the server...

    The Directory In Question on my Shared Hosting Server Only lets me set:

    Set permissions for selected folders.
     Inherit  (Inherit permissions from parent directory)  
     Read   (Directory contents are visible to users)  
     Write   (Applications can write to this directory)  
     
    I have the Read and Write Selected

    I am usually able to delete files at least from within FILEZILLA without a problem.  Only with this one file that I uploaded using the code below. 

    Thanks!!!
     
    ************************************************************************  The Code is Below   ************************************************************

           Dim path As String = Server.MapPath("~/_photo/tempstuff/")
            Dim fileOK As Boolean = False
            If ErrorsExist = True Then
                GoTo ErrorsExisted
            Else
                If upload_Art.HasFile Then
                    Dim fileExtension As String
                    fileExtension = System.IO.Path. _
                        GetExtension(upload_Art.FileName).ToLower()
                    Dim allowedExtensions As String() = _
                        {".jpg"}
                    For i As Integer = 0 To allowedExtensions.Length - 1
                        If fileExtension = allowedExtensions(i) Then
                            fileOK = True
                        End If
                    Next
                    If fileOK Then
                        Try
                            Session("SavedFileName") = (path & _
                                 Session("artID") & fileExtension)

                            upload_Art.PostedFile.SaveAs(path & _
                                 Session("artID") & fileExtension)
                            lblPriceErrors.Text = "File uploaded!"
                        Catch ex As Exception
                            lbl_FileUploadErrors.Text = "File could not be uploaded."
                        End Try
                    Else
                        lbl_FileUploadErrors.Text = "Cannot accept files of this type."
                    End If
                End If
            End If

    Change_Photo_Size:

            If ErrorsExist = True Then
                GoTo ErrorsExisted
            Else
                Try

                    ' Get the path of the original Image
                    Dim displayedImg As String = Session("SavedFileName")

                    ' Get the path of the Thumb folder
                    Dim displayedImgThumb As String = Server.MapPath("~/_photo/finalphotos/")

                    ' Get the original image file name
                    Dim imgFileName As String = System.IO.Path.GetFileName(displayedImg)

                    ' Load original image
                    Dim myimg As System.Drawing.Image = System.Drawing.Image.FromFile(displayedImg)

                    ' Get the thumbnail 170 X 170 px
                    myimg = myimg.GetThumbnailImage(170, 170, Nothing, IntPtr.Zero)

                    ' Save the new thumbnail image
                    myimg.Save(displayedImgThumb & imgFileName, myimg.RawFormat)

                    'now delete the old
                    File.Delete(imgFileName)
                Catch
                End Try

    Wednesday, November 3, 2010 5:23 PM

Answers

  • User-321300854 posted

    This may or may not do the trick, but try calling myImg.Dispose() after saving the image and before trying the delete.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 9, 2010 2:52 PM

All replies

  • User-1787450553 posted

    No problem Sir!!!

    Thursday, November 4, 2010 10:26 AM
  • User-321300854 posted

    This may or may not do the trick, but try calling myImg.Dispose() after saving the image and before trying the delete.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 9, 2010 2:52 PM