views:

303

answers:

3

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.

+1  A: 

Detecting the startup path of a C# windows Form Application

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

SwDevMan81
The OP is looking for how to do it at design time, i.e. within a win forms designer...not at runtime..
tommieb75
A: 

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.

tommieb75
Hi, you can do it, using ITypeResolutionService.GetPathOfAssembly. See: http://msdn.microsoft.com/en-us/library/ms171822%28VS.80%29.aspx. I don't have time to digest it now, but i'll have another look tomorrow and post a solution if no one gets there first.
Jules
digested, and added a solution
Jules
@Jules: Thanks for that...will look into it shortly... cheers. :)
tommieb75
A: 

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.

Jules