我有這樣一個網絡項目:爲什麼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