开发者

How to get the application path at design time in winforms, .net?

开发者 https://www.devze.com 2022-12-17 03:44 出处:网络
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?

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 Nothi开发者_JAVA百科ng as the first parameter to get a list of all types.


Detecting the startup path of a C# windows Form Application

How can i get the application path in C# in a console app


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消