Read build configurations programmatically

2019-07-21 06:43发布

I want to be able to read the VS build configurations programmatically. That is because I want to create my own builder.

How do I do that? Does anyone have code example?

What i mean is that if I have Debug, Development, Release I want them to be listed in a list box in a Form application. I have tried using the "EnvDTE.dll" class but I am not sure it is what I am looking for. If anyone has a concrete example or link to an example I would be more than grateful.

1条回答
叼着烟拽天下
2楼-- · 2019-07-21 07:12

You can use the msbuild API. In Visual Studio 2015, there is a class called Microsoft.Build.Construction.SolutionFile in the Microsoft.Build.dll that ships with VS2015 that you can use to load a solution.

In VS2013 there is no such thing, but you can do the following: (reference 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 contains a list of platforms and configurations contained in the solution. You can use this to populate your forms.

查看更多
登录 后发表回答