Deleting Directory in ASP.net 2.0

系统 1940 0

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.

Deleting Directory in ASP.net 2.0


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论