【问题标题】:Read build configurations programmatically以编程方式读取构建配置
【发布时间】:2015-04-27 19:39:52
【问题描述】:

我希望能够以编程方式读取 VS 构建配置。那是因为我想创建自己的构建器。

我该怎么做?有人有代码示例吗?

我的意思是,如果我有 Debug、Development、Release,我希望它们列在 Form 应用程序的列表框中。我曾尝试使用“EnvDTE.dll”类,但我不确定它是否是我要找的。如果有人有具体示例或示例链接,我将不胜感激。

【问题讨论】:

  • 在文本编辑器中打开您的 .sln 并查找您的配置;您将能够很快弄清楚 VS 是如何做事的。
  • official EnvDTE documentation中有很多样例。我成功地将它用于大型 C++ 解决方案重构,但没有尝试用于 C# 项目。
  • 另一种可能性是使用来自Microsoft.Build 命名空间的类。不确定它是否可以在解决方案级别工作

标签: c# visual-studio msbuild


【解决方案1】:

您可以使用 msbuild API。在 Visual Studio 2015 中,VS2015 附带的 Microsoft.Build.dll 中有一个名为 Microsoft.Build.Construction.SolutionFile 的类,您可以使用它来加载解决方案。

在 VS2013 中没有这样的事情,但你可以这样做: (参考 Microsoft.Build.dll、Microsoft.Build.Engine.dll、Microsoft.Build.Framework.dll)

class Program
{
    static void Main(string[] args)
    {
        string nameOfSolutionForThisProject = @"MySolutionFile.sln";
        string wrapperContent = SolutionWrapperProject.Generate(nameOfSolutionForThisProject, toolsVersionOverride: null, projectBuildEventContext: null);
        byte[] rawWrapperContent = Encoding.Unicode.GetBytes(wrapperContent.ToCharArray());
        using (MemoryStream memStream = new MemoryStream(rawWrapperContent))
        using (XmlTextReader xmlReader = new XmlTextReader(memStream))
        {
            Project proj = ProjectCollection.GlobalProjectCollection.LoadProject(xmlReader);
            foreach (var p in proj.ConditionedProperties)
            {
                Console.WriteLine(p.Key);
                Console.WriteLine(string.Join(", ", p.Value));
            }
        }

        Console.ReadLine();
    }
}

ConditionedProperties 包含解决方案中包含的平台和配置列表。您可以使用它来填充您的表单。

【讨论】:

    猜你喜欢
    • 2016-11-18
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 2023-02-18
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多