2011-05-10 59 views
2

我想獲得我的exe的當前工作目錄。 Directory.GetCurrentDirectory()部署應用程序時不返回exe的工作目錄。在發佈模式下沒有獲取當前工作目錄?

獲取當前工作目錄的方式是什麼?

+2

我懷疑的功能被破壞。所以工作目錄*不是你所期望的,無論出於何種原因。你如何部署應用程序,它是如何啓動的,你期望的目錄*當前目錄是什麼目錄,以及它實際上是什麼目錄*? – 2011-05-10 10:19:40

回答

5


嘗試AppDomain.CurrentDomain.BaseDirectory

+0

+1,不知道該財產存在。 – jgauffin 2011-05-10 10:23:58

+0

@jgauffin:我曾經使用Environment.CurrentDirectory來獲取應用程序的工作目錄,它似乎在開發環境中工作,但是當我創建一個窗口服務並且調用它時,'Environment.CurrentDirectory'返回了system32的路徑因此,爲什麼開始使用我認爲安全的AppDomain.CurrentDomain.BaseDirectory。 – wizzardz 2011-05-10 10:37:31

+0

'Environment.CurrentDirectory'是應用程序啓動的目錄。所有應用程序都可以從任何地方啓動。 – jgauffin 2011-05-10 10:39:38

3

嘗試:

var path = Environment.CurrentDirectory

或:

var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); 
+1

第一個與OP正在嘗試的內容相同,請參閱此處:http://stackoverflow.com/questions/1164756/what-is-the-difference-between-environment-currentdirectory-and-directory-getcurr – 2011-05-10 10:19:40

+0

@ Daniel Hilgarth:謝謝,我不知道。 – jgauffin 2011-05-10 10:22:08

相關問題