Tell if an EnvDTE.Project is a Web Application

A Visual Studio Extension or Add-In might need to know whether or not a project is a web application (in my case, I needed to know whether I’ll expect app.congig or web.config). Due to a bug in Visual Studio, that is not so straight-forward. But thanks to Carlos J. Quintero, I got some code together.

If you need it, suit yourselves.

using System.Linq;
using EnvDTE;

namespace MuniHusseini.Demos
{
        public static bool IsWebProject(this Project project)
        {
            return project.Object is VsWebSite.VSWebSite || project.ProjectHasExtender("WebApplication");
        }

        public static bool ProjectHasExtender(this Project proj, string extenderName)
        {
            // See http://www.mztools.com/articles/2007/mz2007014.aspx for more information.

            try
            {
                // We could use proj.Extender(extenderName) but it causes an exception if not present and 
                // therefore it can cause performance problems if called multiple times. We use instead:

                var extenderNames = (object[])proj.ExtenderNames;

                return extenderNames.Length > 0 && extenderNames.Any(extenderNameObject => extenderNameObject.ToString() == extenderName);
            }
            catch
            {
                // Ignore
            }

            return false;
        }
    }
}

Freelance full-stack .NET and JS developer and architect. Located near Cologne, Germany.

Leave a Reply

Your email address will not be published. Required fields are marked *