2012-08-03 32 views
0

下面是我現在的代碼。它基本上是循環通過項目解決方案項目文件並檢測它是否爲C#文件。但它無法檢測到放在文件夾中的文件,我如何修改它以讀取解決方案文件夾中的C#文件。DTE2 _applicationObject文件夾下的文件名

問候,安迪

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

這將打印解決方案的所有項目,我相信。 它適用於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 + " "); 
      } 
     } 
    } 
} 
相關問題