我有这样一个网络项目:为什么AppDomain.CurrentDomain.BaseDirectory在asp.net应用程序中不包含“bin”?
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;
}
}
}
为什么它的显示...\Web\
而TestProject组件编译成bin
文件夹(换言之它应该在我的想法中显示...\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文件夹只是根的一个子文件夹。 – Tejs 2011-12-29 15:39:37
唷!我以为我疯了!我有同样的问题... – ECC 2015-10-08 12:53:47