组件的Application.executablepath [英] Application.executablepath for component

查看:108
本文介绍了组件的Application.executablepath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在我的应用程序内部(可能在一个Form-Script中)询问'Application.ExecutablePath'时,我得到了答案:

'C:\ Users \ rmeier \documents \ visual studio 2013 \Projects\Rechte \Rechte \ bin \Debug'



这是我想知道的路径。



如果我在同一个表格或模块中放置一个组件内的相同内容我得到答案:

'C:\Program Files (x86)\ Microsoft Visual Studio 12.0 \ Common7 \IDE'



我需要得到与上述相同的结果。

我知道控件和组件或模块之间存在差异,并且组件或模块不直接属于表单/应用程序 - 但组件的应用程序是DevEngine对我来说是一个新的东西。



所以...我的问题是:

如何从应用程序中获取ExecutablePath组件或模块?



我尝试过:



我也尝试用大会的反射IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly.Location)

但这也没有给出正确的答案。

解决方案

问一个问题并且(因为我需要一个解决方案)自己回答(通过大量搜索并阅读大量文章)这是一个很好的练习

这里也是一样......但未来也许其他人也需要这个答案...



关键字是​​DTE - 开发工具环境for Visual Studio。

为此,有必要在参考文献中添加EnvDTE。

第二个关键字是DesignTime - 在运行时来自上面我的问题的方法工作正常。



所以......这是VB代码:

 函数 GetBuildFolder() 作为 字符串 

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

Dim absoluteOutputPath As 字符串
Dim projectFolder 作为 字符串

尝试
' 获取项目的配置管理器
Dim configManager As EnvDTE.ConfigurationManager = myProj.ConfigurationManager

如果 configManager IsNot Nothing 然后

获取有效的项目配置
Dim activeConfiguration As EnvDTE.Configuration = configManager.ActiveConfiguration

' 获取输出文件夹
Dim outputPath As < span class =code-keyword> String = activeConfiguration.Properties.Item( OutputPath)。Value.ToString()

' 输出文件夹可以包含以下模式:
' 1)\\server\folder
' < span class =code-comment> 2)drive:\ folder
' 3).. \..\folder
' 4)文件夹

如果 outputPath.StartsWith(IO.Path.DirectorySeparatorChar& IO.Path.DirectorySeparatorChar)然后
' 情况1:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ outputPath.Length> = 2 AndAlso outputPath.Chars( 1 )= IO.Path.VolumeSeparatorChar 然后
' 情况2:drive:\ folder
absoluteOutputPath = outputPath

ElseIf outputPath.IndexOf( .. \)<> -1 然后
' 就是这种情况3:.. \..\ folder
projectFolder = IO.Path.GetDirectoryName(myProj.FullName)

执行 while outputPath.StartsWith( .. \
outputPath = outputPath.Substring( 3
projectFolder = IO.Path.GetDirectoryName(projectFolder)
循环

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

否则
' 这是案例4:文件夹
projectFol der = IO.Path.GetDirectoryName(myProj.FullName)
absoluteOutputPath = IO.Path.Combine(projectFolder,outputPath)

End 如果

返回 absoluteOutputPath

结束 如果

Catch ex As 异常
结束 尝试

返回

结束 功能


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.

解决方案

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 :

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


这篇关于组件的Application.executablepath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆