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. /p>
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. /p>
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./o:p>/span>/p>
What we
can do not delete the folder and only delete the files in the folder. I.e.
instead of/span>/p>
/p> Directory.Delete(Server.MapPath(path), true);/o:p>/span>/p>
Use/o:p>/span>/b>/p>
filedelete(Server.MapPath(path));/o:p>/span>/p>
where
filedelete() is, (I prefer creating these utilities into function so that I do
not write the same code again and again)/o:p>/span>/b>/p>
private void filedelete(string path) { /span>string[] st; /span>st =
Directory.GetFiles(path);/o:p> /span>int i;/o:p> /span>for (i = 0; i <
st.Length; i++)/o:p> /span>{/o:p> /span>try/o:p> /span>{/o:p>
/span>File.Delete(st.GetValue(i).ToString());/o:p> /span>}/o:p> /span>catch { }/o:p> /span>}/o:p> /span> /span>}/o:p>/span>/p>
/p> 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. /p>
/o:p>/span>/p>
|
Posted on 8/9/2006 2:34:01 AM
This was very Helpful
/p>
|
Posted on 10/9/2006 10:50:57 AM
Hi,
/p>
This is a nice article. We too have similar problem where we used FileDelete(), But never tried with Directory Delete. Thanks for the info.
/p>
Nanda
/p>
|
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.
/p>
|
Posted on 9/6/2007 5:50:58 PM
Hi Sameer,
/p>
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.
/p>
|
Posted on 10/4/2007 2:40:02 AM
|
Posted on 10/30/2007 1:12:01 AM
|
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.
/p>
|
Posted on 4/15/2008 5:55:35 PM
|
Posted on 7/16/2008 2:41:29 AM
many, many thanks! Very helpful! :)
/p>
|
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"
/p>
|
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...
/p>
|
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.
/p>
|