Session e Directory.Delete

Last post 02-18-2009 1:36 PM by Mott. 9 replies.

Sort Posts:

  • Session e Directory.Delete

    12-20-2006, 12:30 PM

    I've discovered that there is this bug in ASP 2.0: when I delete a directory the system close my Session and delete all the session variables that I've created.

     If you know, is there some methods to fix this problem?

    Thanks.

    Igor

  • Re: Session e Directory.Delete

    12-20-2006, 1:45 PM
    • All-Star
      17,453 point All-Star
    • albertpascual
    • Member since 05-23-2003, 2:11 PM
    • Riverside, CA
    • Posts 3,474
    • TrustedFriends-MVPs

    Is not that called an exception?

    Can you add a try and catch around that call?

    Cheers
    Al
    My Blog
    MapStats.NET
    Please click on 'Mark as Answer' if this post answered your question!
  • Re: Session e Directory.Delete

    12-20-2006, 9:39 PM
    • Participant
      1,237 point Participant
    • raymond.wen
    • Member since 11-22-2006, 9:26 AM
    • Posts 242
    Hi, may be it's really a bug, pls check this article up:

    http://www.vikramlakhotia.com/Deleting_Directory_in_ASPnet_20.aspx



    Have you tried to delete a directory programmatically in ASP.Net 2.0? Don’t try it; it can cause great problems to your site especially if you are using in-proc session. You will lose the entire session variable after deleting the directory and there is no way of getting those session variable back. This system is a change from asp.net 1.X.

    I came to know about this the hard way. I was working on a project were project submission was a process of 6 pages (All in one page using the MultiView control). In the last stem I was supposed to delete the entire directory which was created during the process. Strangely I was loosing all the session values when the directories were deleted.

    The problem gets bigger from the fact that the AppDomain will restart again on the first request to that application. So there is no way to restore the session after deleting the directory in the application domain. There is no problem in creating the directory but deleting will hurt the in-proc session.

    What we can do not delete the folder and only delete the files in the folder. I.e. instead of


    Directory.Delete(Server.MapPath(path), true);

    Use

    filedelete(Server.MapPath(path));

    where filedelete() is, (I prefer creating these utilities into function so that I do not write the same code again and again)

    private void filedelete(string path)
    {
    string[] st;
    st = Directory.GetFiles(path);
    int i;
    for (i = 0; i < st.Length; i++)
    {
    try
    {
    File.Delete(st.GetValue(i).ToString());
    }
    catch { }
    }
    }


    I never thought this File Change Notifications (FCN) can cause me so much of problem. So if you find yourself working with file system in DOT Net. Remember deleting a directory can be more than dangerous. So try not to delete any directory at the runtime.

  • Re: Session e Directory.Delete

    12-21-2006, 12:05 AM
    • All-Star
      17,453 point All-Star
    • albertpascual
    • Member since 05-23-2003, 2:11 PM
    • Riverside, CA
    • Posts 3,474
    • TrustedFriends-MVPs
    I run your program without any problem. If you belive that may be a bug please send it to MS. Let's see what can we found!
    Cheers
    Al
    My Blog
    MapStats.NET
    Please click on 'Mark as Answer' if this post answered your question!
  • Re: Session e Directory.Delete

    12-21-2006, 3:30 AM

    Thanks guys for your help.

    This is a very bad thing... I'll use your method to delete the files into the directory and I'll try to "hide" the directory using a system database... Sob!

    If Mr. Bill Gates answer you about this problem, please tell us.

     Bye, Igor.

  • Re: Session e Directory.Delete

    12-21-2006, 10:10 PM
    • Contributor
      6,537 point Contributor
    • bitmask
    • Member since 07-29-2003, 3:18 PM
    • Citizen of the Earth
    • Posts 1,228
    • TrustedFriends-MVPs

    raymond.wen:
    Hi, may be it's really a bug, pls check this article up:

    http://www.vikramlakhotia.com/Deleting_Directory_in_ASPnet_20.aspx


    Unfortunately, it's not a bug - its a "feature".

    In ASP.NET 2.0 the web application will recycle if a subdirectory is added or deleted. This prevents stale content from appearing, but it also means the application will restart and flush out anything in the cache or the session. If you are not serving content from the directory then I'd suggest just moving it outside of the web application directory.

    Scott
    http://www.OdeToCode.com/blogs/scott/
  • Re: Session e Directory.Delete

    12-21-2006, 10:29 PM
    • Star
      10,830 point Star
    • mokeefe
    • Member since 08-20-2006, 5:15 AM
    • Canberra Australia
    • Posts 2,098

    As you are deleting an unused directory it may also be possible that you are creating lots of files dynamically. This will also cause App to recycling to occur.

     

    Rgds,
    Martin.

    For the benefit of all users please mark any post answers as appropriate.
  • Re: Session e Directory.Delete

    12-21-2006, 10:31 PM
    • Star
      10,830 point Star
    • mokeefe
    • Member since 08-20-2006, 5:15 AM
    • Canberra Australia
    • Posts 2,098
    mokeefe:

    As you are deleting an unused directory it may also be possible that you are creating lots of files dynamically. This will also cause App to recycling to occur.

    That is files handled by your application, ASPX etc. It does not include image files; gif, jpg etc.

    Rgds,
    Martin.

    For the benefit of all users please mark any post answers as appropriate.
  • Re: Session e Directory.Delete

    06-11-2008, 4:01 AM
    • Contributor
      6,361 point Contributor
    • anzer
    • Member since 10-19-2004, 4:00 AM
    • UAE
    • Posts 1,284

    mmmm I was trying to figure out why my sessions are expiring soon.. so this is the answer !!!

    Is there any work around to keep the sessions with Directoy.Delete ?

     

    If this post was useful to you, please mark it as answer.

    ClientSideAsp.Net | Blog
  • Re: Session e Directory.Delete

    02-18-2009, 1:36 PM
    • Member
      5 point Member
    • Mott
    • Member since 01-14-2009, 12:07 PM
    • Posts 6

     I've run into this issue as well. Directory.Delete will only end your session if the folder you delete is within the web site. If it's a temp folder outside of the site residing elsewhere on disk, there's no problem. Unfortunately, sometimes it's useful to have the folder within the site, if it has content that should be served to the Internet. But for other temp files, just move the directory outside of the web site.

Page 1 of 1 (10 items)