如何从 VCConfiguration 获取自定义构建工具 属性

How to get Custom Build Tool property from VCConfiguration

目前正在处理 Visual Studio C++ 的扩展,处理使用自定义构建工具构建的文件。在我的一生中,我找不到一种方法来深入了解当前选择的项目的 属性 面板。

场景:

你可以想象它看起来像这样

我目前在项目菜单中有一个按预期执行的命令按钮。我可以获得选定的项目并从中获取选定的项目项目。我将项目的 VCProjectItem 和 VCConfiguration 绑定到活动配置文件。

如何从 属性 页面获取 "Command Line" 属性?

How can I get the "Command Line" property from the property page?

经过深入研究,我发现我们无法使用 属性 VCCustomBuildTool.CommandLine 来获取自定义文件的 属性 命令 .它可用于获取 项目的 属性(右键单击项目)而不是 特定文件的 属性 (右键单击该文件)。

或者换个思路,因为文件是用像CustomBuild这样的节点创建的,所以我们可以在xxxx.vcxproj文件中获取它们。

我们可以使用DTE interface获取当前项目的当前proj文件,然后在父节点custom中读取子节点command构建

举个例子:

        using EnvDTE;
        using System.Xml;

        .........

        VCProject prj; 
        XmlDocument doc = new XmlDocument();
        DTE dTE = Package.GetGlobalService(typeof(DTE)) as DTE;
        prj = dTE.Solution.Projects.Item(1).Object;
        doc.Load(prj.ProjectFile);  //read the proj file for the current project
        XmlElement root = doc.DocumentElement;
        XmlNodeList nodeList = root.GetElementsByTagName("CustomBuild"); //get the customBuild Node
        string str="";
        foreach (XmlNode node in nodeList) //search the child node 'command' in the parent node named custombuild
        {
            str += node["Command"].InnerText.ToString()+"-----";
        }

请不要忘记引用 C:\Program Files (x86)\Microsoft Visual Studio17\Community\Common7\IDE\PublicAssemblies 中存在的 envdte.dll。

希望对您有所帮助。