c# - 以编程方式读取构建配置

标签 c# visual-studio msbuild

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

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

我的意思是,如果我有调试、开发、发布,我希望它们列在表单应用程序的列表框中。我尝试过使用“EnvDTE.dll”类,但我不确定它是我正在寻找的。如果有人有具体的示例或示例链接,我将不胜感激。

最佳答案

您可以使用 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 包含解决方案中包含的平台和配置的列表。您可以使用它来填充您的表单。

关于c# - 以编程方式读取构建配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29904492/

相关文章:

visual-studio - 在 Visual Studio 中使用 Qt 设计器?

asp.net-mvc - 使用 MSBuild 枚举文件夹

msbuild - 使用特定的发布配置文件通过命令行发布控制台应用程序

c# - 释放在 WPF Application.OnStartUp() : Which thread owns it? 中创建的命名互斥体

c# - 使用 ngen.exe 编译内核

.net - 使用 .runsettings 从代码覆盖率中排除程序集

xml - 在PowerShell中使用msbuild在NuGet中的安装事件上编辑csproj

c# - 正则表达式与字符串中的数字匹配

c# - 如何在事件处理程序中设置计时器?

c# - 如何指定通用源文件和属性并让它们出现在 Visual Studio 中