EnvDTE遍历解决方案中的子文件夹

时间:2015-06-10 20:54:54

标签: c# subdirectory envdte

在VS解决方案中有以下结构:

现在,我想参考"目录"文件夹并将其存储在变量中:

        //Solution_Name
        //    ConfigurationObjects
        //        Catalogs
        //        Documents

    foreach (Project item1 in AttachedSolution.Projects)
    {
        if (item1.Kind == ProjectKinds.vsProjectKindSolutionFolder)
        {
            if (item1.Name == "ConfigurationObjects")
            {
                SolutionFolder catalogBaseFolder = (SolutionFolder)item1.Object;

                foreach (ProjectItem item2 in item1.ProjectItems)
                {
                    if ( item2.Name == "Catalogs")
                    {
                        this.CatalogObjectsFolder = (SolutionFolder)item2;
                    // raises error here: Unable to cast COM object of type                   
                    //'System.__ComObject' 
                    //to interface type 'EnvDTE80.SolutionFolder'.
                    };
                };
            };
        };
    };

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

不是第一级的解决方案文件夹被建模为EnvDTE.Project,而不是EnvDTE80.SolutionFolder。您可以使用projectItem.SubProject属性从projectItem获取项目。然后使用Project.Object获取EnvDTE80.SolutionFolder

请参阅:

HOWTO: Navigate the files of a solution from a Visual Studio .NET macro or add-in

How to create a solution folder inside another solution folder