Determine programmatically the index in the solution of project- C#

427 views Asked by At

The method Solution.Projects.Index(Object index) get an index of project as number.

I have the name of the project. How can I determine the index in the solution of this project programmatically?

2

There are 2 answers

5
w.b On BEST ANSWER

You can use linq:

string yourProject = "ProjectName";
var query = Solution.Projects.Cast<Project>()
            .Select((p, i) => new { Name = p.Name, Index = i})
            .First(p => p.Name == yourProject).Index;
2
Yurii On

If you have the name of the project, you could loop through all the projects to find the index of the desired project:

int index = 0;
foreach(Project project in dte.Solution.Projects)
{
    if (string.Equals(project.Name, "desired project name"))
    {
        break;
    }
    index++;
}

On the other hand why do you actually need the index of the project? You can use Item method with project name as parameter as well.

The value passed to index is an integer that is an index to a Project object in its collection. The value of index can alternatively be a string value that equates to the name of a project in the collection.