通过DTE获取项目TargetPath的宏值

发布于 10-29 06:32 字数 494 浏览 6 评论 0原文

我需要通过 DTE 获取项目程序集的绝对输出路径。我尝试使用此方法来执行此操作,我将在其中访问 OutputPath 属性,将其与程序集名称组合,但这会生成相对路径,例如:

<代码>..\..\Output\AnyCPU\Debug\MyAssembly.dll

使用 Path.GetFullPath 对我来说不好,因为我的项目可能是从另一个位置执行的。

我注意到 $(TargetPath) 宏(在项目属性的“生成事件”选项卡中)包含程序集的完整路径。如何从 DTE 以编程方式访问该值?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过度放纵2024-11-05 06:32:54

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

但是,您提到的方法应该仍然可行,因为 OutputPath 属性是相对于项目文件所在的文件夹的。 (如果我错过了一些情况并非如此,请告诉我?)

因此您可以执行类似的操作:(

      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 中可用,但您始终可以向后移植它)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文