Click here to Skip to main content
15,891,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I ask somewhere inside my Application (perhaps in one Form-Script) for the 'Application.ExecutablePath' I get the answer :
'C:\Users\rmeier\documents\visual studio 2013\Projects\Rechte\Rechte\bin\Debug'

This is the path I want to know.

If I ask the same inside a Component which is placed on the same form or a Module I get the answer :
'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE'

I need to get the same result as described above.
I know that there is a difference between Controls and Components or Modules and also that Components or Modules not directly belong to a Form / the Application - but that the Application for a Component is the DevEngine is quite new for me.

So ... my question is :
How do I get the ExecutablePath from the Application inside a Component or a Module ?

What I have tried:

I also tried it with Reflection from the Assembly "IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly.Location)"
But this also doesn't give the right answer.
Posted
Updated 27-Oct-16 1:28am
v2

1 solution

It becomes a nice exercise to ask a question and (because I need a Solution) answer it by myself (by searching a lot and reading a lot of articles)
Here it is the same ... but perhaps someone else in future needs this answer too ...

The Keyword is "DTE" - the "Development Tools Environment" for Visual Studio.
For this it is necessary to add "EnvDTE" to the references.
The 2nd Keyword is DesignTime - at Runtime the methods from my question above are working properly.

So ... here is the VB-Code :
VB
Function GetBuildFolder() As String

    Dim myDTE As DTE = Marshal.GetActiveObject("VisualStudio.DTE")
    Dim myProj As EnvDTE.Project = myDTE.Solution.Projects(0)

    Dim absoluteOutputPath As String
    Dim projectFolder As String

    Try
        ' Get the configuration manager of the project
        Dim configManager As EnvDTE.ConfigurationManager = myProj.ConfigurationManager

        If configManager IsNot Nothing Then

            ' Get the active project configuration
            Dim activeConfiguration As EnvDTE.Configuration = configManager.ActiveConfiguration

            ' Get the output folder
            Dim outputPath As String = activeConfiguration.Properties.Item("OutputPath").Value.ToString()

            ' The output folder can have these patterns:
            ' 1) "\\server\folder"
            ' 2) "drive:\folder"
            ' 3) "..\..\folder"
            ' 4) "folder"

            If outputPath.StartsWith(IO.Path.DirectorySeparatorChar & IO.Path.DirectorySeparatorChar) Then
                ' This is the case 1: "\\server\folder"
                absoluteOutputPath = outputPath

            ElseIf outputPath.Length >= 2 AndAlso outputPath.Chars(1) = IO.Path.VolumeSeparatorChar Then
                ' This is the case 2: "drive:\folder"
                absoluteOutputPath = outputPath

            ElseIf outputPath.IndexOf("..\") <> -1 Then
                ' This is the case 3: "..\..\folder"
                projectFolder = IO.Path.GetDirectoryName(myProj.FullName)

                Do While outputPath.StartsWith("..\")
                    outputPath = outputPath.Substring(3)
                    projectFolder = IO.Path.GetDirectoryName(projectFolder)
                Loop

                absoluteOutputPath = IO.Path.Combine(projectFolder, outputPath)

            Else
                ' This is the case 4: "folder"
                projectFolder = IO.Path.GetDirectoryName(myProj.FullName)
                absoluteOutputPath = IO.Path.Combine(projectFolder, outputPath)

            End If

            Return absoluteOutputPath

        End If

    Catch ex As Exception
    End Try

    Return ""

End Function
 
Share this answer
 
v3
Comments
Karthik_Mahalingam 28-Oct-16 7:50am    
5
Ralf Meier 4-Nov-16 8:10am    
Thank you, Karthik

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900