locked
Looping through ProjectItems in ActiveSolutionProjects RRS feed

  • Question

  • I'm trying to create a C# add-in for VS2010 to count line numbers across an entire solution but I seem to be confused by the ProjectItem setup. For instance if I run the add-in on a C++ solution when I loop through the ProjectItems in a Project I never actually get a code file. I get the vcxproj and the virtual folders such as Source Files and Header Files. 

    This is how I'm looping.

    Array projects = (Array)appObject.ActiveSolutionProjects;
    
    foreach (Project p in projects)
    {
       foreach (ProjectItem i in p.ProjectItems)
       {
         for (int x = 0; x < i.FileCount; x++)
         {
         }
       }
    }
    

    • Moved by OmegaMan Thursday, August 5, 2010 9:31 PM (From:Visual C# General)
    Thursday, August 5, 2010 7:35 PM

Answers

  • I think you need to loop through the Project for ProjectItems, and for each ProjectItem you need to determine whether it has ProjectItems of its own. This is the case when you're looping through, say, an aspx - this ProjectItem consists of 3 Items.

    So you may need something like:

    foreach (ProjectItem item in project.ProjectItems)
    {
    	if (item.ProjectItems.Count > 0)
            	foreach (ProjectItem item in projItems)
    		{
            	if (item.ProjectItems.Count > 0)
                	// do something
    		};
    }

    I suggest you step through the code as you're looping through the ProjectItems, and take a look at each item's ProjectItems collection.

    • Marked as answer by Victor_Chen Monday, August 16, 2010 7:59 AM
    Thursday, August 12, 2010 7:13 PM
  • Hi AridTag,

     

    Thanks for your post.

    In C++ project, a folder is a project item, and a source file is a project item's item.

    There is an easier way to enumerate all source files in a C++ project.

    Would you mind to have a try with "VCProject" Class, reference to:

    http://msdn.microsoft.com/en-us/library/aa712880(VS.71).aspx

    If you'd like to get all items(files & top level folders) in a C++ project, please try property "items":

    http://msdn.microsoft.com/en-us/library/aa652245(v=VS.71).aspx

    On the other hand, if you'd like to collect just files, pleasr try property "files":

    http://msdn.microsoft.com/en-us/library/aa652120(v=VS.71).aspx

    Hope this can help.

     

    Best Regards,

    Ziwei Chen

     

     

    • Edited by Victor_Chen Thursday, August 12, 2010 8:53 AM change to a class support VC++
    • Proposed as answer by Victor_Chen Thursday, August 12, 2010 9:48 AM
    • Marked as answer by Victor_Chen Thursday, August 26, 2010 3:37 AM
    Monday, August 9, 2010 3:53 AM

All replies

  • Hi AridTag,

     

    Thanks for your post.

    In C++ project, a folder is a project item, and a source file is a project item's item.

    There is an easier way to enumerate all source files in a C++ project.

    Would you mind to have a try with "VCProject" Class, reference to:

    http://msdn.microsoft.com/en-us/library/aa712880(VS.71).aspx

    If you'd like to get all items(files & top level folders) in a C++ project, please try property "items":

    http://msdn.microsoft.com/en-us/library/aa652245(v=VS.71).aspx

    On the other hand, if you'd like to collect just files, pleasr try property "files":

    http://msdn.microsoft.com/en-us/library/aa652120(v=VS.71).aspx

    Hope this can help.

     

    Best Regards,

    Ziwei Chen

     

     

    • Edited by Victor_Chen Thursday, August 12, 2010 8:53 AM change to a class support VC++
    • Proposed as answer by Victor_Chen Thursday, August 12, 2010 9:48 AM
    • Marked as answer by Victor_Chen Thursday, August 26, 2010 3:37 AM
    Monday, August 9, 2010 3:53 AM
  • Hi Victor,

    Thanks for the response that seem's to solve the problem with VC projects but what about all other types of projects (VB, C#, asp.net, F# etc...)? This should be as non-specific as possible. The VC++ project I was using was just an example. 

    Thursday, August 12, 2010 6:41 PM
  • I think you need to loop through the Project for ProjectItems, and for each ProjectItem you need to determine whether it has ProjectItems of its own. This is the case when you're looping through, say, an aspx - this ProjectItem consists of 3 Items.

    So you may need something like:

    foreach (ProjectItem item in project.ProjectItems)
    {
    	if (item.ProjectItems.Count > 0)
            	foreach (ProjectItem item in projItems)
    		{
            	if (item.ProjectItems.Count > 0)
                	// do something
    		};
    }

    I suggest you step through the code as you're looping through the ProjectItems, and take a look at each item's ProjectItems collection.

    • Marked as answer by Victor_Chen Monday, August 16, 2010 7:59 AM
    Thursday, August 12, 2010 7:13 PM