Enumerate projects in a solution

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pete Kane

    Enumerate projects in a solution

    Anyone know how to enumerate all projects contained in a solution,
    basically I want to loop through my solution(s) projects and add some
    header info to all my "compileabl e" source files, I know how to retrieve
    project items but need some help with the solution part

    many thanks
  • fmvazque@terra.com.br

    #2
    Re: Enumerate projects in a solution

    Hi Pete,

    It seems that there isn`t a way of doing that by using the classes in
    the MSBUILD API (Microsoft.Buil d.BuildEngine.D ll).

    One of the open-ended issues that Microsoft left in MSBuild is the fact
    that Solution files are *not* valid MSBuild files. So as far as I know
    there is no way to use the MSBuild API to interpret a solution file
    (although you can use it to build an entire solution - Project.Load()
    works with a .sln file).

    One suggestion would be to use a a logic that hacks the .sln internal
    structure in order to discover what projects are part of the solution.
    The following code shows a way to do that:


    const string projectFileLoca tion = @"C:\MySolution .sln";
    StreamReader sr = File.OpenText(p rojectFileLocat ion);

    const string
    matchProjectNam eRegex =
    "^Project\\(\"( ?<PROJECTTYPEGU ID>.*)\"\\)\\s* =\\s*\"(?<PROJE CTNAME>.*)\"\\s *,\\s*\"(?<PROJ ECTRELATIVEPATH >.*)\"\\s*,\\s* \"(?<PROJECTGUI D>.*)\"$";

    List<string> listOfProjects = new List<string>();

    string lineText;
    while ( (lineText = sr.ReadLine()) != null)
    {
    if (lineText.Start sWith("Project( "))
    {
    Match projectNameMatc h = Regex.Match(lin eText,
    matchProjectNam eRegex);
    if (projectNameMat ch.Success)
    {

    listOfProjects. Add(projectName Match.Groups["PROJECTRELATIV EPATH"].Value);
    }
    }
    }

    foreach (string project in listOfProjects)
    {
    ProcessProject( project);
    }

    sr.Close();

    Notice that the code is trying to find the lines that begin with the
    "Project(" string. Those lines describe a projet existing into the
    solution. After that a regular expression is employed to extract the
    path where the project file is located relative to the solution file.

    Regards,
    -----
    Fabio Vazquez [C# MVP]


    Comment

    • Pete Kane

      #3
      Re: Enumerate projects in a solution

      fmvazque@terra. com.br wrote:[color=blue]
      > Hi Pete,
      >
      > It seems that there isn`t a way of doing that by using the classes in
      > the MSBUILD API (Microsoft.Buil d.BuildEngine.D ll).
      >
      > One of the open-ended issues that Microsoft left in MSBuild is the fact
      > that Solution files are *not* valid MSBuild files. So as far as I know
      > there is no way to use the MSBuild API to interpret a solution file
      > (although you can use it to build an entire solution - Project.Load()
      > works with a .sln file).
      >
      > One suggestion would be to use a a logic that hacks the .sln internal
      > structure in order to discover what projects are part of the solution.
      > The following code shows a way to do that:
      >
      >
      > const string projectFileLoca tion = @"C:\MySolution .sln";
      > StreamReader sr = File.OpenText(p rojectFileLocat ion);
      >
      > const string
      > matchProjectNam eRegex =
      > "^Project\\(\"( ?<PROJECTTYPEGU ID>.*)\"\\)\\s* =\\s*\"(?<PROJE CTNAME>.*)\"\\s *,\\s*\"(?<PROJ ECTRELATIVEPATH >.*)\"\\s*,\\s* \"(?<PROJECTGUI D>.*)\"$";
      >
      > List<string> listOfProjects = new List<string>();
      >
      > string lineText;
      > while ( (lineText = sr.ReadLine()) != null)
      > {
      > if (lineText.Start sWith("Project( "))
      > {
      > Match projectNameMatc h = Regex.Match(lin eText,
      > matchProjectNam eRegex);
      > if (projectNameMat ch.Success)
      > {
      >
      > listOfProjects. Add(projectName Match.Groups["PROJECTRELATIV EPATH"].Value);
      > }
      > }
      > }
      >
      > foreach (string project in listOfProjects)
      > {
      > ProcessProject( project);
      > }
      >
      > sr.Close();
      >
      > Notice that the code is trying to find the lines that begin with the
      > "Project(" string. Those lines describe a projet existing into the
      > solution. After that a regular expression is employed to extract the
      > path where the project file is located relative to the solution file.
      >
      > Regards,
      > -----
      > Fabio Vazquez [C# MVP]
      > http://www.phidelis.com.br/blogs/fabiovazquez
      >[/color]

      Hello Fabio, that's what I do at the moment ( although not using regexes
      ), the method I use is IndexOf(".cspro j") parse parse etc..., however
      your method is much more elegant, thanks very much

      Comment

      Working...