Deleting Directory In asp.net 2.0 will make you loose all your session data
Posted on 8/18/2006 10:09:22 AM
in #ASP.NET 2.X
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.
|
Posted on 8/9/2006 2:34:01 AM
|
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
|
Posted on 1/16/2007 6:45:18 PM
|
Posted on 5/29/2007 7:41:54 AM
|
Posted on 8/24/2007 12:44:31 PM
|
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
|
Posted on 10/30/2007 1:12:01 AM
Here is a solution: http://forums.asp.net/p/1144478/1849837.aspx.
Doesn'te work on one off my app pools but it's the only solution i know for now.
|
Posted on 11/19/2007 5:48:11 AM
|
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
|
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 8/25/2008 11:08:13 PM
Dear Vikaram,
This is very nice article.
Vikaram right now i m facing a problem that i need to delete folders which are create inside virtual directory through code and my web site are going to host on sheared hosting where i ll not get full permission, is there any way to manage these things?
Thanking you
Pankaj Lohani
|
Posted on 9/11/2008 7:41:21 AM
Hi Vikram
As you said , i will lose entire session variables if i delete a folder/subfolder under my Web-application,BUT do other concurrent users also lose their session variables when i(or one user) delete a folder or not?
Kind Regards.
|
Posted on 11/17/2008 4:42:21 PM
I have noticed that this does not work in VS 2008 in my GridView. I have HtmlEndoded="false" and my DataFormatString="{0:d}" and still long date with time :(
|
Posted on 11/21/2008 3:58:11 AM
Hi ZarrinPour,
yes, every web client connected to the virtual directory loses the session...
I'm trying to a walkaround right now.
|
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.
|