Search code examples
c#solutionvisual-studio-project

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


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?


Solution

  • 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;