【发布时间】:2012-01-29 23:42:36
【问题描述】:
我有一个类似的网络项目:
namespace Web
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lbResult.Text = PathTest.GetBasePath();
}
}
}
PathTest.GetBasePath() 方法定义在另一个项目中,例如:
namespace TestProject
{
public class PathTest
{
public static string GetBasePath()
{
return AppDomain.CurrentDomain.BaseDirectory;
}
}
}
为什么在 TestProject 程序集编译到 bin 文件夹时显示 ...\Web\(换句话说,在我看来它应该显示 ...\Web\bin)。
现在如果我将方法修改为:
namespace TestProject
{
public class FileReader
{
private const string m_filePath = @"\File.config";
public static string Read()
{
FileStream fs = null;
fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + m_filePath,FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(fs);
return reader.ReadToEnd();
}
}
}
File.config 是在 TestProject 中创建的。现在AppDomain.CurrentDomain.BaseDirectory + m_filePath会返回..\Web\File.config(实际上文件被复制到..\Web\bin\File.config),会抛出异常。
您可以说我应该将m_filePath 修改为@"\bin\File.config"。但是,如果我在您建议的控制台应用程序中使用此方法,AppDomain.CurrentDomain.BaseDirectory + m_filePath 将返回..\Console\bin\Debug\bin\File.config(实际上文件已复制到.\Console\bin\Debug\File.config),由于剩余bin 将引发异常。
换句话说,在网络应用程序中,AppDomain.CurrentDomain.BaseDirectory 是文件复制到的不同路径(缺少/bin),但在控制台应用程序中它是相同的一个路径。
谁能帮帮我?
【问题讨论】:
-
Web 应用程序的基础是包含 ASPX 页面的 Web 根目录。 bin 文件夹只是根目录的子文件夹。
-
呸!我以为我疯了!我有同样的问题...
标签: c# asp.net .net-assembly