2010-01-24 59 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

相关问题