簡體   English   中英

為什么 AppDomain.CurrentDomain.BaseDirectory 在 asp.net 應用程序中不包含“bin”?

[英]Why AppDomain.CurrentDomain.BaseDirectory not contains "bin" in asp.net app?

我有一個網絡項目,如:

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\\bin\\File.config.\\Console\\bin\\Debug\\File.config ),因為bin多余會拋出異常。

換句話說,在 Web 應用程序中, AppDomain.CurrentDomain.BaseDirectory是將文件復制到的不同路徑(缺少/bin ),但在控制台應用程序中它是相同的路徑。
任何人都可以幫助我嗎?

根據 MSDN,應用程序域“代表應用程序域,它是應用程序執行的隔離環境。” 當您考慮 ASP.Net 應用程序時,應用程序所在的根目錄不是 bin 文件夾。 在您的 bin 文件夾中沒有文件是完全可能的,並且在某些情況下是合理的,並且可能根本沒有 bin 文件夾。 由於 AppDomain.CurrentDomain 指的是同一個對象,無論您是從后面的代碼還是從 bin 文件夾中的 dll 調用代碼,您最終都會得到網站的根路徑。

當我編寫了在 asp.net 和 windows 應用程序下運行的代碼時,通常我會創建一個如下所示的屬性:

public static string GetBasePath()          
{       
    if(System.Web.HttpContext.Current == null) return AppDomain.CurrentDomain.BaseDirectory; 
    else return Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"bin");
} 

另一個(未經測試的)選項是使用:

public static string GetBasePath()          
{       
    return System.Reflection.Assembly.GetExecutingAssembly().Location;
} 

如果您想要一個適用於 WinForms 和 Web Apps 的解決方案

    public string ApplicationPath
    {
        get
        {
            if (String.IsNullOrEmpty(AppDomain.CurrentDomain.RelativeSearchPath))
            {
                return AppDomain.CurrentDomain.BaseDirectory; //exe folder for WinForms, Consoles, Windows Services
            }
            else
            {
                return AppDomain.CurrentDomain.RelativeSearchPath; //bin folder for Web Apps 
            }
        }
    }

以上解決方案代碼片段適用於二進制文件位置

AppDomain.CurrentDomain.BaseDirectory仍然是 Web Apps 的有效路徑,它只是web.configGlobal.asax所在的根文件夾,與Server.MapPath(@"~\\");

如果您使用AppDomain.CurrentDomain.SetupInformation.PrivateBinPath而不是BaseDirectory ,那么您應該獲得正確的路徑。

當 ASP.net 構建您的站點時,它會在其特殊位置為它們輸出構建程序集。 所以以這種方式獲得路徑很奇怪。

對於 asp.net 托管應用程序,您可以使用:

string path = HttpContext.Current.Server.MapPath("~/App_Data/somedata.xml");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM