Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine whether or not EnvDTE.Project object represents a C/C++ project?

I have a Project instance, and I don't understand how to find out the project type/language. Specifically, I need to check for a C/C++ project.

The docs seem lacking.

Previously, another person added the following bit of magic to my open-source VS extension, and it worked in VS2013-2015:

private static bool isVisualCppProject(object project)
{
    Type projectObjectType = project.GetType();
    var projectInterface = projectObjectType.GetInterface("Microsoft.VisualStudio.VCProjectEngine.VCProject");
    return projectInterface != null;
}

   ...

isVisualCppProject(project.Object);

But it no longer works in VS 2017 RC. And I would gladly get rid of this runtime reflection magic and un-typed object and dynamic instead of Project - the code is already getting unmaintainable because of this.

like image 595
Violet Giraffe Avatar asked Dec 16 '16 08:12

Violet Giraffe


Video Answer


1 Answers

you can determine project type via project extension name, and the following demo for your reference.

    DTE2 dte = (DTE2)this.ServiceProvider.GetService(typeof(DTE));
            Solution2 solution = (Solution2)dte.Solution;

            foreach (Project p in solution.Projects)
            {

                string[] projectName = p.FullName.Split('.');

                if (projectName.Length > 1)
                {
                   string fileExt = projectName[projectName.Length-1];

                    if (fileExt == "vcxproj")
                    {
                        //is c/c++ porject
                    }
                }
}
like image 53
Zhanglong Wu - MSFT Avatar answered Oct 13 '22 00:10

Zhanglong Wu - MSFT