2011-09-14 34 views
2

我想給Initial目錄提供相應的值,所以它會打開我在項目中創建的文件夾(稱爲「Images」)。 我必須使用相對路徑,所以我的程序不依賴於我工作的計算機。 但問題是我不知道如何訪問此文件夾...使用OpenFileDialog w /相對路徑作爲initialDirectory

有誰知道如何解決這個問題?

回答

7

使用:

openFileDialog.InitialDirectory = Path.Combine(Application.StartupPath, 
@"YourSubDirectoryName"); 

編輯:或者試試這個,如果你喜歡...你在Windows窗體?爲WPF

openFileDialog.InitialDirectory = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Application.ExecutablePath), @"YourSubDirectoryName"); 

編輯2:

string path = System.Reflection.Assembly.GetExecutingAssembly().Location; 

...從Microsoft論壇...

+0

我的應用程序沒有任何StartupPath ...:/ – Marti

+0

(此是一個wpf應用程序,即時通訊實際上尋找的東西給wpf的startupPath) – Marti

+0

事實上,我不知道這是我需要的,因爲我想使用文件在資源文件夾中。 我已經找到了這樣的路徑:/ myProject; component/blabla,但我不記得它是如何工作的... 你知道我的意思嗎? – Marti