[英]Why AppDomain.CurrentDomain.BaseDirectory not contains "bin" in asp.net app?
[英]Why is AppDomain.CurrentDomain.BaseDirectory targeting bin catelog in console application?
我有一個控制台應用程序。 在這個應用程序中,我創建了一個類庫項目,我在主項目中引用了它。 在這個類庫中,我有一個我正在嘗試獲取的文件。 代碼:
public static string GetPath(string fileName)
{
char separator = Path.DirectorySeparatorChar;
string startupPath = AppDomain.CurrentDomain.BaseDirectory;
string[] pathItems = startupPath.Split(separator);
string projectPath = string.Join(separator.ToString(),
pathItems.Take(pathItems.Length - 1)) + ".Handler";
string path = Path.Combine(projectPath, "WebRequestHandlers\\Files" + separator + fileName);
return path;
}
我之前在 .net web 項目中使用過它並且它已經工作但由於某種原因這是我的 AppDomain.CurrentDomain.BaseDirectory:
C:\\projects\\OverWatchServerCall\\OverWatchServerCall\\bin\\Debug\\
它應該是:
C:\\projects\\OverWatchServerCall\\OverWatchServerCall\\
該文件在 classlibrary 項目中,正如我之前所說,這在 .net web api 項目中工作正常
為什么會這樣?
編輯:
Directory.GetCurrentDirectory()
返回相同的路徑
AppDomain.CurrentDomain.BaseDirectory
不是由程序集定義的,而是由宿主環境(即應用程序或服務)定義的。
構建控制台應用程序后,可執行文件和附屬程序集將轉到*.csproj
文件中定義的輸出路徑。 默認為bin\\$(Configuration)
(例如: bin\\Debug
)。
由於整個可執行文件都在輸出路徑中執行,因此這是基本目錄。
為什么在 ASP.NET WebAPI 項目中在您的情況下運行良好? 可能是因為您在 IIS 中托管 ASP.NET WebAPI。 如果您使用托管在 Windows 服務中的 OWIN/Katana 來執行此操作,您最終會遇到同樣的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.