【问题标题】:How to Get Project Type Guid of selected project in the Solution Explorer by using VS Package如何使用 VS Package 在解决方案资源管理器中获取选定项目的项目类型 Guid
【发布时间】:2017-04-24 17:00:28
【问题描述】:

我创建了简单的 VS 包,用于在解决方案资源管理器的上下文菜单中添加新项目。我需要检查选定项目的项目类型 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”。

谁能帮帮我?

【问题讨论】:

    标签: vspackage vs-extensibility


    【解决方案1】:

    我已经找到了答案,

    参考: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;
            }
        }
    

    它可以满足我的要求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-04
      • 2012-10-12
      相关资源
      最近更新 更多