DTE2 _applicationObject读取文件夹中的文件名

时间:2012-08-03 08:58:16

标签: add-in envdte

下面是我目前的代码。 它的作用基本上是通过项目解决方案项目文件循环并检测它是否是C#文件。但是它无法检测放在文件夹中的文件,如何修改它以读取解决方案文件夹中的C#文件。

问候,安迪

  foreach (var projectItem in
            _applicationObject.Solution.Projects.Cast<Project>().SelectMany(project => project.ProjectItems.Cast<ProjectItem>()))
        {
            //for (var i = 0; i < projectItem.FileCount; i++)
            //{


            if (projectItem.FileCount > 0 && projectItem.Name.EndsWith(".cs")) // check if project is .Cs files
            {
                string fileName;
                try
                {

                    fileName = projectItem.FileNames[0];
                }
                catch (Exception)
                {
                    continue;
                }
                //end of find filename

            }


        }

1 个答案:

答案 0 :(得分:1)

我相信这将打印解决方案中的所有项目。 它适用于VS 2012中的C ++解决方案。

    // XXX  Test
    IEnumerator enumerator = m_applicationObject.Solution.GetEnumerator();
    string indent = "  ";
    while (enumerator.MoveNext())
    {
        Project p = enumerator.Current as Project;
        if (p != null)
        {
            Debug.WriteLine(p.Name);
            ProcessProjectItems(p.ProjectItems, indent);
        }
    }


// XXX  Test
void ProcessProjectItems(ProjectItems pis, string indent)
{
    if (pis == null)
        return;

    IEnumerator items = pis.GetEnumerator();
    while (items.MoveNext())
    {
        ProjectItem pi = items.Current as ProjectItem;
        if (pi != null)
        {
            Debug.WriteLine(indent + pi.Name);

            if (pi.ProjectItems != null)
            {
                ProcessProjectItems(pi.ProjectItems, indent + "  ");
            }
            else
            {
                Project p = pi.Object as Project;
                if (p != null && p.ProjectItems != null)
                    ProcessProjectItems(p.ProjectItems, indent + "  ");
            }
        }
    }
}