Results 1 to 4 of 4

Thread: Cannot delete a file (File being used by another process) * Resolved *

  1. #1

    Thread Starter
    Hyperactive Member MarkusJ_NZ's Avatar
    Join Date
    Jun 2001
    Posts
    375

    Cannot delete a file (File being used by another process) * Resolved *

    Hi, I have a web application that spits out the following error when I try and delete a file from a code-behind page.

    "The process cannot access the file c:\path\blah.jpg" because it is being used by another process"

    If I wait a little while and try agiain then I can successfully delete the file from the exact same page.

    I have declared everything referencing the file to nothing but I still can't delete the file without waiting a few minutes

    Help appreciated

    Cheers

    =============

    The following is a subset of the code that I was having problems with. It was fixed by adding in

    fileGetImage.Dispose()
    fileGetImage = Nothing
    System.GC.Collect

    HTH anyone who has a similiar problem
    Cheers
    MarkusJ

    VB Code:
    1. Protected WithEvents fileGetImage As System.Web.UI.HtmlControls.HtmlInputFile
    2.  
    3. if page.isPostback then
    4. fileGetImage.PostedFile.SaveAs("c:\imagename.jpg")
    5. fileGetImage.Dispose()
    6. fileGetImage = Nothing
    7. ' Call the garbage collection
    8. System.GC.Collect
    9. end if
    10.  
    11. ' You can now delete the image
    12. System.IO.File.delete("c:\imagename.jpg")
    Last edited by MarkusJ_NZ; Sep 16th, 2003 at 10:56 PM.

  2. #2
    Lively Member
    Join Date
    Nov 2001
    Posts
    85

    Same Problem

    I have the exact same problem and haven't been able to find a solution. Did you resolve this issue?

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Maybe forcing a garbage collection would solve this. I have also came across this problem, but found ways around it. I never tried garbage collection though, so who knows.
    http://msdn.microsoft.com/library/de...collection.asp
    http://msdn.microsoft.com/library/de...collection.asp

    If you get it working, please post back what works.

  4. #4
    Lively Member
    Join Date
    Nov 2001
    Posts
    85

    Resolved

    I encountered this problem when I create a file and delete it soon after. If I create the file using File.Create() or FileInfo.Create() I get the error when I delete the file that is created. If I create the file using the following code, I don't get the error...

    Code:
    Dim fsUpdateFile As New FileStream(strFile, FileMode.OpenOrCreate)
    fsUpdateFile.Flush()
    fsUpdateFile.Close()
    I don't know if it will help anyone else, but it seems to have solved my problems.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width