我用.net工具开发Asp.net画面,浏览过程中,想在root目录(c:\interpub\wwwroot\xxxx\)上写一个叫test.log的文件。我用了这么几种方法,以及得出的结果:
Request.PhysicalApplicationPath
结果:c:\inetpub\wwwroot\WebTemplate\
Request.ApplicationPath
结果:C:\WebTemplate\ -_-!!
不用Request方法,直接写文件,看看默认路径,居然是
结果:c:\winnt\system32 ......附上源代码:
Log.cs Log类using System;
using System.IO;namespace WebTemplate
{
/// <summary>
/// 生成5层级别的Log,存入Log文件当中。
/// </summary>
public class Log
{
String _str_LogFilePath;

/**
 * 构造函数,配置log文件存放路径
 **/ 
public Log(String aStrLogFilePath)
{
_str_LogFilePath = aStrLogFilePath;
} /**
 * 构造函数,使用默认的log文件存放路径
 **/ 
public Log()
{
_str_LogFilePath = "test.log";
} /**
 * 配置文件存放路径
 **/ 
public void setLogFilePath(String aStrLogFilePath)
{
_str_LogFilePath = aStrLogFilePath;
} /**
 * 向文件追加一行String
 **/ 
protected void addLine(String aStrLog)
{
using ( StreamWriter sw = new StreamWriter(_str_LogFilePath, true))
{
sw.WriteLine("[" + DateTime.Now + "] " + aStrLog);
}
} /**
 * 添加Fatal级别Log
 **/ 
public void Fatal(String aStrLog)
{
String strLog = "| FATAL :" + aStrLog;
addLine(strLog);
} /**
 * 添加Error级别Log
 **/ 
public void Error(String aStrLog)
{
String strLog = "| ERROR :" + aStrLog;
addLine(strLog);
}

/**
 * 添加Warn级别Log
 **/ 
public void Warn(String aStrLog)
{
String strLog = "|  WARN :" + aStrLog;
addLine(strLog);
} /**
 * 添加Info级别Log
 **/ 
public void Info(String aStrLog)
{
String strLog = "|  INFO :" + aStrLog;
addLine(strLog);
} /**
 * 添加Debug级别Log
 **/
public void Debug(String aStrLog)
{
String strLog = "| DEBUG :" + aStrLog;
addLine(strLog);
}
}
}TempForm.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace WebTemplate
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid WebDataGrid;
private static Log _log;

private void Page_Load(object sender, System.EventArgs e)
{
_log = new Log(Request.ApplicationPath + "\\Test.log");
_log.Debug("Test init dbm log......");
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.WebDataGrid.SelectedIndexChanged += new System.EventHandler(this.WebDataGrid_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void WebDataGrid_SelectedIndexChanged(object sender, System.EventArgs e)
{

}
}
}