Getting the macro value of project's TargetPath via DTE

一个人想着一个人 提交于 2019-12-07 12:10:30

问题


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

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

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?


回答1:


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

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
                                  });
    }

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



来源:https://stackoverflow.com/questions/5486593/getting-the-macro-value-of-projects-targetpath-via-dte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!