stevensvs5
stevensvs5

Reputation: 23

Configuration.Properties object returning null in Visual Studio 2017 VSIX extension

I built an extension for Visual Studio 2015 using a VSIX project template. It adds a button in the context menu when you right click on a project in the solution explorer. When you click on this button, it changes some of the debugging settings for that project so that it is ready to debug.

However, I also need to make it compatible with Visual Studio 2017. Everything seems to work fine in terms of the button appearing in the menu. I use the dte2 object to get the currently selected items and then I use that to get the selected project. From that I get the active configuration and use that to change the properties of the project.

Everything works fine up until I get the configuration object. I can get the object, but when I try to get configuration.properties, it returns back null.

DTE2 dte2 = (DTE2)Package.GetGlobalService(typeof(SDTE));
SelectedItem selectedItem = dte2.SelectedItems.Item(1);
Project project = selectedItem.Project;

Configuration configuration = project.ConfigurationManager.ActiveConfiguration;
string path = configuration.Properties.Item("OutputPath").value.ToString();

Everything works fine up until I try to get the Properties from configuration. It returns null, so I am getting an error when I use .Item().

This same code worked for me in Visual Studio 2015, but it isn't working in Visual Studio 2017. Does anyone know any solutions or work arounds to this?

This is the only related link I found after quite a bit of google research. But here the only response from the visual studio team is that they don't have enough information.

EDIT: This is what I ended up working for me:

DTE2 dte2 = (DTE2)Package.GetGlobalService(typeof(SDTE));
SelectedItem selectedItem = dte2.SelectedItems.Item(1);
Project project = selectedItem.Project;

VCProject vcproj = project.Object as VCProject;
VCConfiguration vcconfig = vcproj.ActiveConfiguration;
VCDebugSettings vcdebug = vcconfig.DebugSettings as VCDebugSettings;

Then the VCDebugSettings object can edit the debug properties of the project. The available properties can be found here

Upvotes: 1

Views: 1167

Answers (1)

Zhanglong Wu - MSFT
Zhanglong Wu - MSFT

Reputation: 1660

Does anyone know any solutions or work arounds to this?

If your project is c++ project, you can use VCProject to get the configuration object. like this:

 private void GetProjectProperties(object sender, EventArgs e)
        {
            DTE2 dte = (DTE2)Package.GetGlobalService(typeof(SDTE));
            VCProject prj = dte.Solution.Projects.Item(1).Object as VCProject;
            foreach (VCConfiguration vccon in prj.Configurations)
            {
                IVCRulePropertyStorage generalRule = vccon.Rules.Item("ConfigurationGeneral");

                string outputPath = vccon.OutputDirectory;

                vccon.OutputDirectory = "$(test)";
                //string test1 = generalRule.GetEvaluatedPropertyValue(2);
                string tar = generalRule.GetEvaluatedPropertyValue("TargetExt");
                string name = generalRule.GetEvaluatedPropertyValue("TargetName");
            }
}

Upvotes: 1

Related Questions