如何在Session_End事件里得到网站的物理目录?
作者:孟宪会 发布日期:2010-06-01 14:23:04

在Session_End事件里,只有Application、Server和Session对象可以使用,但Server.MapPath()方法是不能使用的。要得到网站的物理路径,Server.MapPath("~")是无法得到的。一个解决办法是可以写在web.config文件里,也可以在Application_Start事件里定义一个全局的静态变量,例如:

C# 代码
private static String WebSiteRootPath = System.String.Empty;
void Application_Start(object sender, EventArgs e)
{
  WebSiteRootPath
= Server.MapPath("~");
}
void Session_End(object sender, EventArgs e)
{
  System.IO.StreamWriter s
= new System.IO.StreamWriter(WebSiteRootPath + "log.txt");
  s.WriteLine(
"Session_End:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  s.WriteLine(
"SessionID:" + Session.SessionID);
  s.Close();
}

 也可以直接使用 HttpRuntime.AppDomainAppPath 属性和 System.Web.Hosting.HostingEnvironment.MapPath("~/") 方法得到。

原文地址:http://dotnet.aspx.cc/article/f47beb03-4161-4999-8c22-197d894b7639/print.aspx
© 版权所有 【孟宪会之精彩世界】TM 2012