2012-04-16 33 views
2

我已經創建了用於在解決方案資源管理器的上下文菜單中添加新項目的簡單VS包。在這我需要檢查選定項目的項目類型GUID。我怎麼能得到這個。如何使用VS包在Solution Explorer中獲取所選項目的項目類型Guid

例如,One Solution包含三種不同類型的項目,如WindowFormsApplication,MVC Projects,WebApplication。在選擇MVC項目時,我們需要獲取該ProjectType GUID。

我已經試過了以下的我Package.cs,

IVsMonitorSelection monitorSelection = (IVsMonitorSelection)Package.GetGlobalService(typeof(SVsShellMonitorSelection)); 

monitorSelection.GetCurrentSelection(out hierarchyPtr, out projectItemId, out mis, out selectionContainerPtr); 

IVsHierarchy hierarchy = Marshal.GetTypedObjectForIUnknown(hierarchyPtr, typeof(IVsHierarchy)) as IVsHierarchy; 

if (hierarchy != null) 
{ 
    object prjItemObject; 
    hierarchy.GetProperty(projectItemId, (int)__VSHPROPID.VSHPROPID_ExtObject, out prjItemObject); 
    string projectTypeGuid; 
     Project prjItem = prjItemObject as Project; 
     projectTypeGuid = prjItem.Kind; 
} 

在我得到GUID作爲 「FAE04EC0-301F-11D3-BF4B-00C04F79EFBC」 爲所有選定的項目。

任何人都可以幫助我嗎?

回答

5

我已經找到答案了這一點,

參考:https://www.mztools.com/articles/2007/MZ2007016.aspx

public string GetProjectTypeGuids(EnvDTE.Project proj) 
     { 
      string projectTypeGuids = ""; 
      object service = null; 
      Microsoft.VisualStudio.Shell.Interop.IVsSolution solution = null; 
      Microsoft.VisualStudio.Shell.Interop.IVsHierarchy hierarchy = null; 
      Microsoft.VisualStudio.Shell.Interop.IVsAggregatableProject aggregatableProject = null; 
      int result = 0; 
      service = GetService(proj.DTE, typeof(Microsoft.VisualStudio.Shell.Interop.IVsSolution)); 
      solution = (Microsoft.VisualStudio.Shell.Interop.IVsSolution)service; 

      result = solution.GetProjectOfUniqueName(proj.UniqueName, out hierarchy); 

      if (result == 0) 
      { 
       aggregatableProject = (Microsoft.VisualStudio.Shell.Interop.IVsAggregatableProject)hierarchy; 
       result = aggregatableProject.GetAggregateProjectTypeGuids(out projectTypeGuids); 
      } 

      return projectTypeGuids; 
     } 

     public object GetService(object serviceProvider, System.Type type) 
     { 
      return GetService(serviceProvider, type.GUID); 
     } 

     public object GetService(object serviceProviderObject, System.Guid guid) 
     { 
      object service = null; 
      Microsoft.VisualStudio.OLE.Interop.IServiceProvider serviceProvider = null; 
      IntPtr serviceIntPtr; 
      int hr = 0; 
      Guid SIDGuid; 
      Guid IIDGuid; 

      SIDGuid = guid; 
      IIDGuid = SIDGuid; 
      serviceProvider = (Microsoft.VisualStudio.OLE.Interop.IServiceProvider)serviceProviderObject; 
      hr = serviceProvider.QueryService(ref SIDGuid, ref IIDGuid, out serviceIntPtr); 

      if (hr != 0) 
      { 
       System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(hr); 
      } 
      else if (!serviceIntPtr.Equals(IntPtr.Zero)) 
      { 
       service = System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(serviceIntPtr); 
       System.Runtime.InteropServices.Marshal.Release(serviceIntPtr); 
      } 

      return service; 
     } 
    } 

及其對我的要求正常工作。

+1

取自http://www.mztools.com/articles/2007/MZ2007016.aspx – Artiom 2014-01-20 10:19:08