Deleting Directory In asp.net 2.0 will make you loose all your session data

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.



Share this post   Email it

Feedback

Posted on 8/9/2006 2:34:01 AM

This was very Helpful

Posted on 10/9/2006 10:50:57 AM

Hi,

This is a nice article. We too have similar problem where we used FileDelete(), But never tried with Directory Delete. Thanks for the info.

Nanda

Posted on 10/17/2006 7:06:38 AM

Thank you!

Posted on 1/16/2007 6:45:18 PM

Thanks a lot!

Posted on 5/29/2007 7:41:54 AM

Thanks a lot!

Posted on 8/24/2007 12:44:31 PM

Thanks a lot!

Posted on 9/6/2007 1:03:27 PM

Vikram, this article explains that deleting a directory will cause a problem, but does not offer any suggestions as to other ways to get around this problem other than to simply not delete the directory.. However, if deleting the directory is the requirement, then here is an idea - create your folder outside of the application root and then if you delete it, it should be no problem. However, this might not work for you depending on the requirements of your application.

Posted on 9/6/2007 5:50:58 PM

Hi Sameer,

I could not find any good workaround for this. Yes keeping the directory outside the virtual directory will work but this will not be feasible in most cases.

Posted on 10/4/2007 2:40:02 AM

Thanks

Posted on 11/19/2007 5:48:11 AM

Thanks it works !!!

Posted on 11/20/2007 2:42:24 AM

Thank you, very useful! I've almost got mad solving this.

Posted on 4/15/2008 5:55:35 PM

THanks

Posted on 7/16/2008 2:41:29 AM

many, many thanks! Very helpful! :)

Posted on 7/21/2008 2:20:09 PM

But this still does not work for detailview control, I have same column which has date, it works in GridView even without setting HtmlEncode to false, however, for the detailView, even I set the HtmlEncode to "false", it still show date like this "8//10//2007 12:00:00 AM"

Posted on 4/1/2009 1:20:36 PM

I sure everyone has this answer, but if not add EnableSessionState="True" and the problem will go away...

Posted on 9/18/2009 7:46:10 PM

Actually, EnableSessionState is set to true by default, so you don't have to set it. And it does not make the "problem" go away as this behavior is by design by Microsoft.

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Copyright © 2006 - 2012 Vikram Lakhotia