2011-12-29 41 views
20

我有這樣一個網絡項目:爲什麼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)的不同路徑,但在控制檯應用程序中它是相同的一個路徑。
任何人都可以幫助我嗎?

+0

Web應用程序的基礎是包含ASPX頁面的Web根目錄。 bin文件夾只是根的一個子文件夾。 – Tejs 2011-12-29 15:39:37

+0

唷!我以爲我瘋了!我有同樣的問題... – ECC 2015-10-08 12:53:47

回答

28

根據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; 
} 
+0

一個很好的解決方法,但它不回答爲什麼....也許沒有好的答案。 – kenny 2011-12-29 15:59:58

+0

@kenny - 用一點點信息更新 – Peter 2011-12-29 16:20:15

+1

您的回答非常明確且樂於助人,非常感謝! – 2011-12-30 01:17:57

14

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

+1

注意,根據MSDN PrivatBinPath可能是一個comman分隔字符串的文件夾。 http://msdn.microsoft.com/en-us/library/system.appdomainsetup.privatebinpath.aspx – Peter 2011-12-29 15:50:35

+0

+1這是迄今爲止最好的答案@Peter FWIW在實踐中它似乎並不符合我的觀察。尚未準備好將其稱爲完整的文檔錯誤,但 – 2013-07-02 10:41:56

+0

非常感謝 – Neo 2015-07-01 10:21:18

2

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

對於asp。淨託管的應用程序,你可以使用:

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

如果您想,對於WinForms和Web應用程序

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.asaxServer.MapPath(@"~\");相同

相關問題