通过DTE获取项目的TargetPath的宏值 [英] Getting the macro value of project's TargetPath via DTE

查看:94
本文介绍了通过DTE获取项目的TargetPath的宏值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过DTE获取项目程序集的绝对输出路径.我尝试使用此方法,我将在其中访问OutputPath属性并将其与程序集名称组合在一起,但是这会产生相对路径,例如:

I need to get the absolute output path of the project's assembly via DTE. I tried doing this using this method, where I would access the OutputPath property, combining it with the assembly name, however this produces the relative path, such as:

..\..\Output\AnyCPU\Debug\MyAssembly.dll

使用Path.GetFullPath对我不利,因为我的项目可能正在其他位置执行.

Using Path.GetFullPath is not good for me, because my project might be executing from another location.

我注意到$(TargetPath)宏(在项目属性的构建事件"选项卡中)包含程序集的完整路径.如何从DTE以编程方式访问此值?

I noticed that the $(TargetPath) macro (in Build Events tab in project properties) contains the full path of the assembly. How can I access this value programmatically from the DTE?

实际问题是-如何获得项目的绝对输出路径?

Actual question is - how do I get the absolute output path of the project?

推荐答案

我不知道如何以编程方式访问"$(TargetPath)",我同意那可能是最好的解决方案.

I don't know how to programmatically access the "$(TargetPath)", I agree that that could've been the best solution.

但是,您提到的方法应该仍然可行,因为OutputPath属性是相对于项目文件所在的文件夹的. (请让我知道是否丢失了不是这种情况的情况?)

However, the approach you mentioned should still be workable,since the OutputPath property is relative to the folder in which the project file resides. (Please let me know if I'm missing some scenario where this is not the case?)

因此,您可以执行以下操作:

So you can do something similar to this:

      private static string GetProjectExecutable(Project startupProject, Configuration config)
    {
        string projectFolder    = Path.GetDirectoryName(startupProject.FileName);
        string outputPath       = (string)config.Properties.Item("OutputPath").Value;
        string assemblyFileName = (string)startupProject.Properties.Item("AssemblyName").Value + ".exe";
        return Path.Combine(new[] {
                                      projectFolder,
                                      outputPath,
                                      assemblyFileName
                                  });
    }

(此处使用的Path.Combine的重载仅在.NET 4.0中可用,但您始终可以将其反向移植)

(the overload of Path.Combine used here is only available in .NET 4.0 but you could always backport it)

这篇关于通过DTE获取项目的TargetPath的宏值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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