【问题标题】:How to get the application path at design time in winforms, .net?如何在设计时在winforms,.net中获取应用程序路径?
【发布时间】:2011-01-08 16:45:46
【问题描述】:

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

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

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

ETA:我在下面发布了解决方案:

ETA2:

因为它是相关的,我想我会发布另一个有用的设计时服务的 sn-p。与下面的解决方案一样,此示例适用于 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 将包含从 MyType 派生的所有类型。将第二个参数更改为 True 以排除搜索 GAC。 将 Nothing 作为第一个参数传递以获取所有类型的列表。

【问题讨论】:

    标签: c# .net winforms path design-time


    【解决方案1】:

    【讨论】:

    • OP 正在寻找如何在设计时执行此操作,即在 win 表单设计器中......而不是在运行时......
    【解决方案2】:

    这是从 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 的代码,您可以在其中找到大量信息。

    【讨论】:

    • 你有同样的 c# 解决方案吗?
    【解决方案3】:

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

    希望这会有所帮助, 最好的祝福, 汤姆。

    【讨论】:

    • 嗨,你可以做到,使用 ITypeResolutionService.GetPathOfAssembly。请参阅:msdn.microsoft.com/en-us/library/ms171822%28VS.80%29.aspx。我现在没有时间消化它,但我明天再看一下,如果没有人先到那里,我会发布解决方案。
    • 消化了,补充了解决办法
    • @Jules:谢谢你...很快就会调查...干杯。 :)
    • @t0mm13b 如果您正在创建用户控件并希望在设计时访问项目文件夹中的文件,这很有意义
    猜你喜欢
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 2011-05-16
    • 2014-11-22
    • 1970-01-01
    • 2020-05-25
    • 2011-01-26
    相关资源
    最近更新 更多