Deleting directory in Asp.net 2.0

Source: Internet
Author: User

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 thatThe 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

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 configurications (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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.