2010-01-24 58 views
0

如果在引用的dll中使用Application.StartupPath,則路徑指向IDE的路徑。如何在winforms,.net中的設計時獲得應用程序路徑?

是否有無法獲得實際應用程序的路徑?

只是要清楚,這是在設計時。

ETA:我已經發布了以下的解決方案:

ETA2:

因爲它是相關的,我想我會發布另一個有用的設計時服務的一個片段。像下面的解決方案一樣,此示例適用於UITypeEditor:

Public Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object 

    Dim typeDiscovery As ITypeDiscoveryService = TryCast(provider.GetService(GetType(ITypeDiscoveryService)), ITypeDiscoveryService) 
    Dim types As ICollection = typeDiscovery.GetTypes(GetType(MyType), False) 

End Function 

類型將包含從MyType派生的所有類型。將第二個參數更改爲True以排除搜索GAC。 傳遞Nothing作爲獲取所有類型列表的第一個參數。

回答

0

以下是如何從UITypeEditor中完成的。

ETA:原代碼有一個額外的不必要的步驟。我忘了我們有一個服務提供者,所以不需要看這個網站。流線型的代碼是:

Public Class MyEditor 
    Inherits UITypeEditor 

    Public Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object 

     Dim typeRes As ITypeResolutionService = TryCast(provider.GetService(GetType(ITypeResolutionService)), ITypeResolutionService) 
     Dim ass As System.Reflection.AssemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName() 
     MessageBox.Show(ass.CodeBase, "Design-time Path") 
     MessageBox.Show(typeRes.GetPathOfAssembly(ass), "Run-time Path") 

    End Function 

End Class 

原始代碼:

Public Class MyEditor 
    Inherits UITypeEditor 

    Public Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object 

     Dim component As IComponent = TryCast(context.Instance, IComponent) 
     Dim site As ISite = component.Site 
     Dim typeRes As ITypeResolutionService = TryCast(site.GetService(GetType(ITypeResolutionService)), ITypeResolutionService) 
     Dim ass As System.Reflection.AssemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName() 
     MessageBox.Show(ass.CodeBase, "Design-time Path") 
     MessageBox.Show(typeRes.GetPathOfAssembly(ass), "Run-time Path") 

    End Function 

End Class 

該解決方案是基於代碼的How to: Access Design-Time Services,在這裏您可以找到豐富的信息。

-1

你不能在設計時做到這一點!在運行時你可以,也就是當你構建應用程序並運行它,或者在VS中點擊F5或點擊綠色箭頭。爲什麼你想在設計時知道?這是無關緊要的,因爲可執行文件及其相關的DLL沒有真正加載到內存中並執行,而且,如果對代碼進行了更改,整個項目將不得不重新進行重新構建。

希望這會有所幫助, 最好的問候, 湯姆。

+0

嗨,你可以使用ITypeResolutionService.GetPathOfAssembly來做到這一點。請參閱:http://msdn.microsoft.com/en-us/library/ms171822%28VS.80%29.aspx。我現在沒有時間消化它,但是如果沒有人先到那裏,我會明天再看一次併發佈一個解決方案。 – Jules 2010-01-24 03:12:38

+0

消化,並添加了一個解決方案 – Jules 2010-01-24 12:32:47

+1

@Jules:謝謝你... ...將很快研究它......歡呼聲。 :) – t0mm13b 2010-01-24 13:30:08

相關問題