0

我的问题与这个问题非常相似:How to cast ComObject to ENVDTE.Project?

我想处理在 Visual Studio -> 解决方案资源管理器中选择的项目项。如果项目已加载,代码工作正常,但卸载项目有问题(它们被称为未建模项目(http://msdn.microsoft.com/en-us/library/hw7ek4f4%28v=vs.80%29.aspx) .

为已加载项目uiItem.Object 投射选定项目是 EnvDTE.Project很好,但如何投射未建模项目?没有“UnmodeledProject”类,并且转换uiItem.Object 是 ProjectItem不起作用。

这是我的代码:

Window solutionExplorer = mApplicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer);
if(solutionExplorer != null)
{
    UIHierarchy uiHierarchy = (UIHierarchy)solutionExplorer.Object;
    if (uiHierarchy != null)
    {
        object[] selectedItems = (object[])uiHierarchy.SelectedItems;
        foreach (UIHierarchyItem uiItem in selectedItems)
        {                              
            // Valid project
            if (uiItem.Object is EnvDTE.Project)
            {
                EnvDTE.Project project = uiItem.Object as EnvDTE.Project;
                if (project.FullName.Contains(".vdproj") || project.Kind == "{54435603-DBB4-11D2-8724-00A0C9A8B90C}")
                {

                }
            }
            else if (uiItem.Object is ProjectItem)
            {
              // This is never jumped...
            }
            else
            {  ...
4

2 回答 2

2

由于我没有找到解决这种情况的方法,所以我使用了这个技巧:

string pathToVdProject = null;
try
{
    Window solutionExplorer = mApplicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer);
    if (solutionExplorer != null)
    {
        UIHierarchy uiHierarchy = (UIHierarchy)solutionExplorer.Object;
        if (uiHierarchy != null)
        {
            object[] selectedItems = (object[])uiHierarchy.SelectedItems;
            foreach (UIHierarchyItem uiItem in selectedItems)
            {
                // Valid project
                if (uiItem.Object is EnvDTE.Project)
                {
                    EnvDTE.Project project = uiItem.Object as EnvDTE.Project;
                    if (project.FullName.Contains(".vdproj") || project.UniqueName.Contains(".vdproj")
                        || (String.Compare(project.Kind, ProjectsGuids.guidVdSetupProject, true) == 0))                                    
                    {
                        // Valid Project has property FullName which is full path to .vdproj file
                        pathToVdProject = project.FullName;
                        break;
                    }
                }
                else if (uiItem.Object is ProjectItem)
                {
                    // This never happens...
                }
                else
                {
                    // This is a little tricky: Unmodeled Projects cannot be casted to EnvDTE.Project http://msdn.microsoft.com/en-us/library/hw7ek4f4%28v=vs.80%29.aspx 
                    Solution2 solution = (Solution2)mApplicationObject.Solution;

                    // So get all projects in solution (including unmodeled) and try to find a match by name
                    foreach (Project project in solution.Projects)
                    {
                        if (project.Kind == EnvDTE.Constants.vsProjectKindUnmodeled)
                        {
                            // Unmodeled project found (Normal projects are recognized in 'uiItem.Object is EnvDTE.Project'
                            if (project.Name.Contains(uiItem.Name))
                            {
                                // This is 'Project' for selected item
                                if (project.Name.Contains(".vdproj") || project.UniqueName.Contains(".vdproj"))
                                {
                                    // Unmodeled projects does not offer property FullName and UniqueName does NOT contain full path to file!
                                    FileInfo fileInfo = new FileInfo(solution.FullName);

                                    // Create full path from solution (.sln) path and project relative path
                                    pathToVdProject = fileInfo.DirectoryName + "\\" + project.UniqueName;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
于 2013-06-21T05:55:15.523 回答
0

List of all loaded/Unloaded projects inside the solution explorer will be available in your EnvDTE application object. Without using solution Explorer window and UIHierarchy i got the project details. Below code snippets working fine for me. Please check out weather it will fit for you..

For Each item As EnvDTE.Project In mApplicationObject.Solution.Projects
     If item.Globals Is Nothing AndAlso item.Object Is Nothing Then
        Console.WriteLine(item.Name + "  is currently unloaded!")
     End If
Next
于 2013-06-04T09:40:26.470 回答