我有一個控制檯應用程序。在這個應用程序中,我創建了一個我在主項目中引用的類libarary項目。在這個類庫中,我有一個我正在嘗試獲取的文件。這個代碼:爲什麼AppDomain.CurrentDomain.BaseDirectory在控制檯應用程序中將bin catelog作爲目標?
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:\項目\ OverWatchServerCall \ OverWatchServerCall \ BIN \調試\
它應該是:
C:\項目\ OverWatchSe rverCall \ OverWatchServerCall \
的文件是在classlibrary項目,如我之前說的這個作品中的.NET Web API項目精細
這究竟是爲什麼?
編輯:
Directory.GetCurrentDirectory()
返回相同的路徑
謝謝你的補償。我正在尋找一個解決方案,但在我感到沮喪的時候,我在forgott提到它。然而我用substring刪除/斌/調試和我的代碼工作 – ThunD3eR
@ Ra3IDeN沒問題!不客氣,我很高興你解決了這個問題;) –
@ Ra3IDeN這裏沒有修復,因爲'BaseDirectory'只能在AppDomain被創建時提供:( –