ASP.NET:删除文件夹后会话过期

本文关键字:会话 过期 文件夹 删除 NET ASP | 更新日期: 2023-09-27 18:35:22

我有代码将文件移动到另一个文件夹并删除 ASP.NET 应用程序中以前的文件夹和文件。删除文件夹后,我的会话已过期。如何限制过期会话值。

ASP.NET:删除文件夹后会话过期

非常正确。发生这种情况是因为删除操作会更改 ASP.NET 应用程序的文件夹树,这会强制应用程序回收。看这里: http://www.geekays.net/post/2008/10/14/ASPNET-webdomain-recycle-on-subfolder-changes.aspx

在 Global.asax 页面中使用以下代码

void Application_Start(object sender, EventArgs e) 
{
    System.Reflection.PropertyInfo p = typeof(System.Web.HttpRuntime).GetProperty("FileChangesMonitor", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
    object o = p.GetValue(null, null);
    System.Reflection.FieldInfo f = o.GetType().GetField("_dirMonSubdirs", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.IgnoreCase);
    object monitor = f.GetValue(o);
    System.Reflection.MethodInfo m = monitor.GetType().GetMethod("StopMonitoring", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
    m.Invoke(monitor, new object[] { }); 
}

请在globle.asax中Session_End方法中编写目录删除代码

void Session_End(object sender, EventArgs e)
{
    SampleDelete();
}
private static void SampleDelete()
{
    try
    {
        string samplesss = AppDomain.CurrentDomain.BaseDirectory + "''temp";
        string[] array1 = System.IO.Directory.GetDirectories(samplesss);
        try
        {
            foreach (string item in array1)
            {
                System.IO.Directory.Delete(item, true);
            }
        }
        catch (Exception ex)
        { }
    }
    catch (Exception ex)
    {
    }
}