Folders not deleting immediately

Discussion in 'ASP .Net' started by jawloc@hotmail.com, Feb 26, 2007.

  1. Guest

    Hi I have a question that I hope someone can help me with. I have
    created a web page that allows a user to upload an image with a
    caption to my web server. Here is what happens when the user loads
    the page:


    1 - A temporary folder is created with that user's username here is an
    example of the folder "c:\wwwroot\upload\tmp\username".

    2 - The user uploads the file it gets saved in the temp folder.

    3 - The user hits the save button and the caption is dumped to the
    database and then a new permanent folder is created (example "c:
    \wwwroot\upload\article_43"). So now the file stored in the temp
    folder gets copied to the permanent folder and then the content of the
    temp folder is deleted along with the temp folder.

    This is where I run into a problem. My temporary folder does not
    delete immediately if at all. But it does not give me an error. If I
    try to do anything with that folder from that point on I get an access
    denied error. If I stop the IIS server the temp folder disappears and
    everything is as it should be.

    Either I'm not using the proper code to create or delete my files and
    folders or its something to do with IIS or my web.config.

    Here is my helper subs I use to create copy and delete.
    ================================================================

    Public Sub CheckPath(ByVal Path As String)
    Dim Mydirectory As New DirectoryInfo(Path)
    If Not Mydirectory.Exists Then
    Mydirectory.Create()
    End If
    End Sub

    Public Sub CopyDir(ByVal FromPath As String, ByVal ToPath As
    String)
    Dim toDir As New DirectoryInfo(ToPath)
    Dim fromDir As New DirectoryInfo(FromPath)
    If toDir.Exists Then
    toDir.Delete(True)
    toDir.Create()
    Else
    toDir.Create()
    End If
    If fromDir.Exists Then
    For Each theFiles As FileInfo In fromDir.GetFiles
    Dim TargetFile As String = ToPath + "\" +
    theFiles.Name
    theFiles.CopyTo(TargetFile, True)
    Next
    End If
    End Sub

    Public Sub DelDir(ByVal DirPath As String)
    Dim DelDir As New DirectoryInfo(DirPath)
    If DelDir.Exists Then
    DelDir.Delete(True)
    End If
    End Sub

    Public Sub delFile(ByVal FullPath As String)
    Dim TargetFile As New FileInfo(FullPath)
    If TargetFile.Exists Then
    TargetFile.Delete()
    End If
    End Sub

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

    Any help would be apreciated.

    Thanks in advance
    Eric
     
    , Feb 26, 2007
    #1
    1. Advertising

  2. On Feb 26, 9:29 pm, wrote:
    > Hi I have a question that I hope someone can help me with. I have
    > created a web page that allows a user to upload an image with a
    > caption to my web server. Here is what happens when the user loads
    > the page:
    >
    > 1 - A temporary folder is created with that user's username here is an
    > example of the folder "c:\wwwroot\upload\tmp\username".
    >
    > 2 - The user uploads the file it gets saved in the temp folder.
    >
    > 3 - The user hits the save button and the caption is dumped to the
    > database and then a new permanent folder is created (example "c:
    > \wwwroot\upload\article_43"). So now the file stored in the temp
    > folder gets copied to the permanent folder and then the content of the
    > temp folder is deleted along with the temp folder.
    >
    > This is where I run into a problem. My temporary folder does not
    > delete immediately if at all. But it does not give me an error. If I
    > try to do anything with that folder from that point on I get an access
    > denied error. If I stop the IIS server the temp folder disappears and
    > everything is as it should be.
    >
    > Either I'm not using the proper code to create or delete my files and
    > folders or its something to do with IIS or my web.config.
    >
    > Here is my helper subs I use to create copy and delete.
    > ================================================================
    >
    > Public Sub CheckPath(ByVal Path As String)
    > Dim Mydirectory As New DirectoryInfo(Path)
    > If Not Mydirectory.Exists Then
    > Mydirectory.Create()
    > End If
    > End Sub
    >
    > Public Sub CopyDir(ByVal FromPath As String, ByVal ToPath As
    > String)
    > Dim toDir As New DirectoryInfo(ToPath)
    > Dim fromDir As New DirectoryInfo(FromPath)
    > If toDir.Exists Then
    > toDir.Delete(True)
    > toDir.Create()
    > Else
    > toDir.Create()
    > End If
    > If fromDir.Exists Then
    > For Each theFiles As FileInfo In fromDir.GetFiles
    > Dim TargetFile As String = ToPath + "\" +
    > theFiles.Name
    > theFiles.CopyTo(TargetFile, True)
    > Next
    > End If
    > End Sub
    >
    > Public Sub DelDir(ByVal DirPath As String)
    > Dim DelDir As New DirectoryInfo(DirPath)
    > If DelDir.Exists Then
    > DelDir.Delete(True)
    > End If
    > End Sub
    >
    > Public Sub delFile(ByVal FullPath As String)
    > Dim TargetFile As New FileInfo(FullPath)
    > If TargetFile.Exists Then
    > TargetFile.Delete()
    > End If
    > End Sub
    >
    > ================================================================
    >
    > Any help would be apreciated.
    >
    > Thanks in advance
    > Eric


    Looks like the file or directory is locked somewhere and you cannot
    delete it until the server is restarted.

    Try to delete all files from the directory one by one. Maybe it will
    show you if there any problem with lock, or access...

    So, instead of

    DelDir.Delete(True)

    this code

    For Each fn As String in Directory.GetFiles(DirPath)
    File.Delete(fn)
    Next
    DelDir.Delete
     
    Alexey Smirnov, Feb 26, 2007
    #2
    1. Advertising

  3. Guest

    Thanks for the reply Alex. I've tried what you have suggested and
    the files themself aren't locked they delete no problem. If i skip
    the deletion of the folder itself everything works ok. I just dont
    want to have a million empty folders after running the site for any
    extended period of time. It's only the folders that seems locked.
    Even if browse to the folder with windows explorer and I select it I
    immediately get an access denied error.

    Perhaps there is some type of auditing or health monitoring that is
    being applies to the upload folder? If so is there a way to remove the
    auditing from the upload folder and it's subfolders?

    One thing I do know for sure is that its not an NTFS rights thing
    cause it happens even on a fat32 system.

    Any thoughts are appreciated.
    Eric
     
    , Feb 26, 2007
    #3
  4. On Feb 27, 12:17 am, wrote:
    > Thanks for the reply Alex. I've tried what you have suggested and
    > the files themself aren't locked they delete no problem. If i skip
    > the deletion of the folder itself everything works ok. I just dont
    > want to have a million empty folders after running the site for any
    > extended period of time. It's only the folders that seems locked.
    > Even if browse to the folder with windows explorer and I select it I
    > immediately get an access denied error.
    >
    > Perhaps there is some type of auditing or health monitoring that is
    > being applies to the upload folder? If so is there a way to remove the
    > auditing from the upload folder and it's subfolders?
    >
    > One thing I do know for sure is that its not an NTFS rights thing
    > cause it happens even on a fat32 system.
    >
    > Any thoughts are appreciated.
    > Eric


    I found the reason.

    This is due to File Change Notifications (FCN) and affected the .NET 2
    only

    http://blogs.msdn.com/toddca/archive/2005/12/01/499144.aspx
    http://www.vikramlakhotia.com/Deleting_Directory_in_ASPnet_20.aspx

    So, don't delete directories at the runtime

    By the way. If this is not working as expected, why don't you change
    the logic? You can have only one initial directory at c:\wwwroot\upload
    \tmp\ and use it for all files from all users. To remember what the
    user has uploaded you can use either session, or a database, or a
    special naming of the files (e.g. article_43_thefilename.jpg). Once a
    new permanent folder is created you delete all files from the current
    user...
     
    Alexey Smirnov, Feb 27, 2007
    #4
  5. Patrice Guest

    Another option also is to don't do this under the application root. Or to
    use a virtual directory.

    This way ASP.NET 2.0 won't consider that you changed the application storage
    layout...

    "Alexey Smirnov" <> a écrit dans le message de news:
    ...
    > On Feb 27, 12:17 am, wrote:
    >> Thanks for the reply Alex. I've tried what you have suggested and
    >> the files themself aren't locked they delete no problem. If i skip
    >> the deletion of the folder itself everything works ok. I just dont
    >> want to have a million empty folders after running the site for any
    >> extended period of time. It's only the folders that seems locked.
    >> Even if browse to the folder with windows explorer and I select it I
    >> immediately get an access denied error.
    >>
    >> Perhaps there is some type of auditing or health monitoring that is
    >> being applies to the upload folder? If so is there a way to remove the
    >> auditing from the upload folder and it's subfolders?
    >>
    >> One thing I do know for sure is that its not an NTFS rights thing
    >> cause it happens even on a fat32 system.
    >>
    >> Any thoughts are appreciated.
    >> Eric

    >
    > I found the reason.
    >
    > This is due to File Change Notifications (FCN) and affected the .NET 2
    > only
    >
    > http://blogs.msdn.com/toddca/archive/2005/12/01/499144.aspx
    > http://www.vikramlakhotia.com/Deleting_Directory_in_ASPnet_20.aspx
    >
    > So, don't delete directories at the runtime
    >
    > By the way. If this is not working as expected, why don't you change
    > the logic? You can have only one initial directory at c:\wwwroot\upload
    > \tmp\ and use it for all files from all users. To remember what the
    > user has uploaded you can use either session, or a database, or a
    > special naming of the files (e.g. article_43_thefilename.jpg). Once a
    > new permanent folder is created you delete all files from the current
    > user...
    >
     
    Patrice, Feb 27, 2007
    #5
  6. Jawloc Guest

    Thank you both for your help.

    I do belive you are correct about the File Change Notification causing
    this issue.

    I will try the virtual directory thing first and if that gives a hard
    time i guess i will need to put a few things back to the drawing board
    and use one temp forlder for everyone.

    Again thanks for your help,
    Eric.
     
    Jawloc, Feb 27, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    609
  2. master007
    Replies:
    8
    Views:
    862
    Oliver Wong
    Mar 6, 2006
  3. Harry Barker
    Replies:
    2
    Views:
    544
    Alf P. Steinbach
    Apr 19, 2006
  4. Replies:
    1
    Views:
    245
  5. tkondal
    Replies:
    0
    Views:
    246
    tkondal
    Jul 18, 2007
Loading...

Share This Page