![]() |
||||
|
Introduction This article provides information about how to guess the type of a project (Windows, executable or class library, Web Site, Web application, etc.) from a Visual Studio macro or add-in. You can have, at least, the following project types:
More Information The first property to use to determine the type of a project is EnvDTE.Project.Kind, which returns a Guid (since Visual Studio is so extensible, it must accommodate unknown project types and therefore it can not be an enum). You must add some assembly references such as VSLangProj.dll, etc. to your project to get the definition of some well-known Guid values and other classes or interfaces. See the article INFO: Assemblies used in Visual Studio Extensibility. Among others, EnvDTE.Project.Kind can take these values:
Also, you can check the availability of project extenders to guess the type of a project. You can check the presence of an extender in a project with this function:
Public Function ProjectHasExtender(ByVal proj as EnvDTE.Project, ByVal extenderName As String) As Boolean
Dim result As Boolean = False
Dim extenderNames() As Object
Dim extenderNameObject As Object
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:
extenderNames = DirectCast(proj.ExtenderNames, Object())
If extenderNames.Length > 0 Then
For Each extenderNameObject In colExtenderNames
If extenderNameObject.ToString() = extenderName Then
result = True
Exit For
End If
Next
End If
Catch
' Ignore
End Try
Return result
End Function
public bool ProjectHasExtender(EnvDTE.Project proj, string extenderName)
{
bool result = false;
object[] extenderNames;
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:
extenderNames = (object[]) proj.ExtenderNames;
if (extenderNames.Length > 0)
{
foreach (object extenderNameObject in extenderNames)
{
if (extenderName.ToString() == extenderName)
{
result = true;
break;
}
}
}
}
catch
{
// Ignore
}
return result;
}
Some well-known extender names are:
You can distinguish Windows from Web applications using the EnvDTE.Project.Properties.Item("ProjectType") property, which can take these values:
For Windows applications you can guess the type using the property EnvDTE.Project.Properties.Item("OutputType"), which can take these values:
To guess the .NET language of a project see the article HOWTO: Get the language of a project or file from a Visual Studio .NET macro or add-in. The ultimate way of guessing the type / subtype of a project is to get its Guids. See the article HOWTO: Get the project flavour (subtype) of a Visual Studio project from an add-in. Related articles
Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||