Daniel Fisher  lennybacon
Daniel Fisher lennybacon

Reputation: 4194

Detect a dotnet core project from EnvDTE.Project API

I try to figure out if the current project (e.g. dte.ActiveSolutionProjects[0]) is a .NET core project.

From the XML of the csproj file it can be told by looking at the project node attributes:

a) Normal .NET

<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

b) donet Core:

<Project Sdk="Microsoft.NET.Sdk">

But how to get that information from the Project API?

I could not find hint looking at the MSDN docs (they target Visual Studio 2015) or exploring the API while debugging...

Upvotes: 2

Views: 910

Answers (3)

Julian
Julian

Reputation: 1

Following worked for us. A bit more performance hungry and uses the Microsoft.Build package.

        private static Microsoft.Build.Evaluation.Project GetProjectFromGlobalProjectCollection(EnvDTE.Project proj)
        {
            return Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection.LoadedProjects.FirstOrDefault(p => p.FullPath == proj.FullName);
        }

        private static Microsoft.Build.Evaluation.Project GetMSBuildProject(EnvDTE.Project proj)
        {
            var project = GetProjectFromGlobalProjectCollection(proj);
            if (project is null)
            {
                return new Microsoft.Build.Evaluation.Project(proj.FullName);
            }
            return project;
        }

        /// <summary>
        /// Checks whatever a project has Sdk style or not
        /// </summary>
        /// <param name="proj"></param>
        /// <returns>true if sdk style</returns>
        public static bool IsSdkStyleProject(EnvDTE.Project proj)
        {
            var msBuildProject = GetMSBuildProject(proj);
            switch (msBuildProject.Xml.Sdk)
            {
                case "Microsoft.NET.Sdk": return true;
                case "Microsoft.NET.Sdk.Web": return true;
                case "Microsoft.NET.Sdk.WindowsDesktop": return true;
                default: return false;
            }
        }

Upvotes: 0

Daniel Fisher  lennybacon
Daniel Fisher lennybacon

Reputation: 4194

Since August 2018 (VS 15.8) the previously voted answer does not work any more.

@reduckted posted a link to the vs developer community where David Kean [MSFT] posted a possible solution:

using Microsoft.VisualStudio.Shell;

internal static bool IsCpsProject(this IVsHierarchy hierarchy)
{
  Requires.NotNull(hierarchy, "hierarchy");
  return hierarchy.IsCapabilityMatch("CPS");
}

Upvotes: 0

ErikEJ
ErikEJ

Reputation: 41819

You should be able to use

Project.Kind

C# .NET Project: {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}

C# dotnet Core Project: {9A19103F-16F7-4668-BE54-9A1E7A4F7556}

Upvotes: 4

Related Questions