2010-06-08 102 views
2

我在.net的winform項目中有一個名爲reports的目錄。我的項目名稱是AccountingReports,並且存在Directory目錄報告。所以我需要通過代碼訪問此路徑的方式。在Asp.net中,我們使用Request.PhysicalApplicationPath屬性。那麼,有沒有任何方法或屬性存在,這將使我的根我的項目如何獲取我的WinForm應用程序項目中的目錄路徑

回答

8

的你可以使用:

Directory.GetCurrentDirectory

獲取應用程序的當前工作目錄。

然後,您可以使用附加的 「報告」 到以下幾點:

Path.Combine(string, string)

Dim reportsFolder As String 
reportsFolder = Path.Combine(Directory.GetCurrentDirectory(), "Reports") 
+0

仍然給我\ bin \ debug [可執行文件的當前工作目錄],所以我需要把報告放到調試目錄中來測試我的應用程序...&進一步安裝的位置將是AppInstallDir/Reports /它的工作...我是對的嗎? 沒有其他辦法銘記.. – 2010-06-08 11:06:21

+0

@Amit - 是的,除非你把位置放到app.config目錄並從那裏讀取。 – ChrisF 2010-06-08 11:52:00

+0

然後在這種情況下,在部署之後,用戶每次移動Reports目錄時都需要更新app.config文件。 – 2010-06-08 12:13:37

1

當IDE與安裝我用運行:

如果System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed - 如果已安裝,則爲true false IDE

已安裝 - System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory

IDE - My.Application.Info.DirectoryPath

+0

仍然相同My.Application.Info.DirectoryPath = \ bin \ debug – 2010-06-08 12:17:20

+0

無論如何,我需要做一些解析字符串或複製粘貼到可執行目錄..通常感謝您的支持.. – 2010-06-08 12:19:20

+0

它總是有趣得到downvotes在一個六年的職位,沒有任何解釋... – dbasnett 2016-03-14 23:18:49

5
Dim path As String = AppDomain.CurrentDomain.BaseDirectory 
'replace \bin\Debug\ the part of the path you dont want with part you do want 
Dim path1 As String = path.Replace("\bin\Debug\", "\htmlFiles\") 
+0

這是唯一一個在開發VSTO加載項時爲我工作的人。因爲沒有抓住這個,所有人都感到羞恥。 – Lopsided 2016-03-14 05:35:39

0

嘛。可能遲了一點,但我只是有同樣的問題,並像這樣解決:

Dim Path As String() = Directory.GetCurrentDirectory.ToString.Split(New Char() {"\"c}) 
    For i = 0 To Path.Count - 3 
     PathLb.Text = PathLb.Text & "\" & Path(i) 
    Next i 
    PathLb.Text = PathLb.Text.Remove(0, 1) 

有了這個路徑包含在'PathLb'。

希望它對任何人都有用。

相關問題