如何在winforms、.net中获取设计时的应用程序路径?

发布于 2024-08-18 22:36:55 字数 733 浏览 4 评论 0原文

如果在引用的dll中使用Application.StartupPath,则该路径指向IDE的路径。

有没有办法获取实际应用程序的路径?

需要明确的是,这是在设计时。

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 作为第一个参数以获取所有类型的列表。

If you use Application.StartupPath in a referenced dll, the path points to the path of the IDE.

Is there anyway to get the path of the actual application?

Just to be clear, this is at design time.

ETA: I've posted the solution below:

ETA2:

Because it's related, I thought i'd post a snippet of another useful design-time service. Like the solution below, this example is for a 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

types will contain all types derived from MyType. Change the second parameter to True to exclude searching the GAC.
Pass Nothing as the first parameter to get a list of all types.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖上得阳光 2024-08-25 22:36:55

以下是如何从 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

此解决方案基于 如何:访问设计时服务,您可以在其中找到大量信息。

Here's how to do it from a UITypeEditor.

ETA: The original code has an extra unneeded step. I forgot we have a serviceprovider, so no need to look at the site. The streamlined code is:

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

Original Code:

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

This solution is based on code at How to: Access Design-Time Services, where you'll find a wealth of information.

顾北清歌寒 2024-08-25 22:36:55

你不能在设计时做到这一点!在运行时,即当您构建应用程序并运行它时,或者在 VS 中按 F5 或单击绿色箭头时,您可以。为什么你想在设计时知道?这是无关紧要的,因为可执行文件及其相关的 DLL 并未真正加载到内存中并执行,此外,如果对代码进行更改,则必须重新构建整个项目。

希望这有帮助,
此致,
汤姆.

You cannot do it at design-time! At runtime you can, that is, when you build the application and run it, or hitting F5 within VS or clicking on the green arrow. Why would you want to know at design-time? It is irrelevant as the executable and its related DLL's are not really loaded into memory and executed, plus, if a change was made to the code, the whole project would have to be rebuilt again.

Hope this helps,
Best regards,
Tom.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文