Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Visual Studio Add-in Library

, 15 Sep 2010
This library allows you to quickly and simply create add-ins for VS2008 and VS2010 (VS2005?).
VSAddInLibrary_src.zip
TestAddin
Resources
1.ico
2.bmp
serviceunknown.ico
TestAddin.AddIn
TestAddin.csproj.user
VisualStudio Add-in Library Sources
Helpers
Menus
Actions
Base
Properties
VisualStudio Add-in Library.csproj.user
using System;
using System.Collections.Generic;
using EnvDTE80;

namespace VisualStudio.AddInLibrary.Helpers
{
  public enum VStudioCSharpProjectType { Unknown = 0, WinFormConsole, WebApplication, MVCApplication, WebSite };


  public class ProjectType
  {
    public string Name {get;set;}
    public string Id { get; set; }
    public VStudioCSharpProjectType VSType { get; set; }
    public string ResourceFolder { get; set; }
  }

  

  public static class VStudioHelpers
  {
    // public DTE2 VStudioApplication { get; set; }

    // chek with Project.kind property
    public static List<ProjectType> GetSupportedProjectTypes()
    {
      List<ProjectType> tmp = new List<ProjectType>();
      tmp.Add(new ProjectType() { VSType=VStudioCSharpProjectType.WinFormConsole, Name = "C# Winform / Console ", Id = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}", ResourceFolder="Properties" });
      tmp.Add(new ProjectType() { VSType = VStudioCSharpProjectType.WebApplication, Name = "C# Web Application Project Templates", Id = "{349C5853-65DF-11DA-9384-00065B846F21}", ResourceFolder = "App_GlobalResources" });
      tmp.Add(new ProjectType() { VSType = VStudioCSharpProjectType.MVCApplication, Name = "C# Web MVC Project Templates", Id = "{603c0e0c-db56-11dc-be95-000d561079b0}", ResourceFolder = "App_GlobalResources" });
      tmp.Add(new ProjectType() { VSType = VStudioCSharpProjectType.WebSite, Name = "Web Site", Id = "{E24C65DC-7377-472B-9ABA-BC803B73C61A}", ResourceFolder="" });
      return 	tmp; 	 	
    }

    public static List<string> GetSupportedCodeModels()
    {      
      List<string> tmp = new List<string>();
      tmp.Add(EnvDTE.CodeModelLanguageConstants.vsCMLanguageCSharp);      
      return 	tmp; 	 	
    }

    public static bool IsSupportedProject(EnvDTE.Project p, out ProjectType ProjectTypeFound)
    {
      bool ret = false;
      ProjectTypeFound = null;
      if (p == null) return ret;
      List<string> languages = GetSupportedCodeModels();
      if (p.CodeModel.Language != null && languages.IndexOf(p.CodeModel.Language) >= 0)
      {
        string projectType = p.Kind;
        if (!String.IsNullOrEmpty(projectType))
        {
          string[] types = projectType.Split(new char[] {';' });
          if (types.Length == 0) types = new string[1] { projectType };

          List<ProjectType> support = GetSupportedProjectTypes();
          for (int i = 0; i < types.Length; i++)
          {
            ProjectType found = support.Find(
                 delegate(ProjectType inP) { return (String.Compare(inP.Id,types[i],true)==0); }
               );
            if (found != null)
            {
              ProjectTypeFound = found;
              ret = true;
              break;
            }
          }          
        }        
      }

      return ret;
    }


    public static bool isSolutionLoaded(DTE2 _applicationObject)
    {
      bool ret = false;
      if (_applicationObject != null && _applicationObject.Solution != null)
      {
        ret = true;
      }
      return ret;
    }

    public static bool SolutionHasSupportedProjects(DTE2 _applicationObject)
    {
      bool ret = false;
      if (isSolutionLoaded(_applicationObject))
      {
        EnvDTE.Projects projects =  _applicationObject.Solution.Projects;
        if (projects != null)
        {
          ProjectType ProjectTypeFound;
          foreach (EnvDTE.Project p in projects)
          {
            if (IsSupportedProject(p, out ProjectTypeFound))
            {
              ret = true;
              break;
            }
          }
        }
      }
      return ret;
    }

    public static EnvDTE.Project GetActiveProject(DTE2 _applicationObject)
    {
      EnvDTE.Project p = null;
      if (isSolutionLoaded(_applicationObject))
      {
        Array projectArray = null ;
        try{
          projectArray = (Array) _applicationObject.ActiveSolutionProjects ;
          if (projectArray!=null && projectArray.Length>0)
            p = (EnvDTE.Project) projectArray.GetValue(0);
        }
        catch
        {
        }      
      }
      return p;
    }

    public static bool IsActiveProjectSupported(DTE2 _applicationObject)
    {
      bool bsupport = false;
      ProjectType pfound = null;
      EnvDTE.Project p = GetActiveProject(_applicationObject);
      if (p != null)
        bsupport = VStudioHelpers.IsSupportedProject(p, out pfound);
      return bsupport;
    }

    public static string GetProjectPath(EnvDTE.Project p)
    {
      string ret = String.Empty;
      if (p == null) return ret;
      try
      {
        ret = System.IO.Path.GetDirectoryName(p.FullName);
      }
      catch
      {
      }

      return ret;
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

KL@DotNet
Program Manager Digitas
France France
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 15 Sep 2010
Article Copyright 2010 by KL@DotNet
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid