364,033 Members | 4770 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Enumerate projects in a solution

Pete Kane
P: n/a
Pete Kane
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 "compileable" source files, I know how to retrieve
project items but need some help with the solution part

many thanks
Apr 25 '06 #1
Share this Question
Share on Google+
2 Replies


fmvazque@terra.com.br
P: n/a
fmvazque@terra.com.br
Hi Pete,

It seems that there isn`t a way of doing that by using the classes in
the MSBUILD API (Microsoft.Build.BuildEngine.Dll).

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 projectFileLocation = @"C:\MySolution.sln";
StreamReader sr = File.OpenText(projectFileLocation);

const string
matchProjectNameRegex =
"^Project\\(\"(?<PROJECTTYPEGUID>.*)\"\\)\\s*=\\s* \"(?<PROJECTNAME>.*)\"\\s*,\\s*\"(?<PROJECTRELATIV EPATH>.*)\"\\s*,\\s*\"(?<PROJECTGUID>.*)\"$";

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

string lineText;
while ( (lineText = sr.ReadLine()) != null)
{
if (lineText.StartsWith("Project("))
{
Match projectNameMatch = Regex.Match(lineText,
matchProjectNameRegex);
if (projectNameMatch.Success)
{

listOfProjects.Add(projectNameMatch.Groups["PROJECTRELATIVEPATH"].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

Apr 26 '06 #2

Pete Kane
P: n/a
Pete Kane
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.Build.BuildEngine.Dll).
>
> 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 projectFileLocation = @"C:\MySolution.sln";
> StreamReader sr = File.OpenText(projectFileLocation);
>
> const string
> matchProjectNameRegex =
> "^Project\\(\"(?<PROJECTTYPEGUID>.*)\"\\)\\s*=\\s* \"(?<PROJECTNAME>.*)\"\\s*,\\s*\"(?<PROJECTRELATIV EPATH>.*)\"\\s*,\\s*\"(?<PROJECTGUID>.*)\"$";
>
> List<string> listOfProjects = new List<string>();
>
> string lineText;
> while ( (lineText = sr.ReadLine()) != null)
> {
> if (lineText.StartsWith("Project("))
> {
> Match projectNameMatch = Regex.Match(lineText,
> matchProjectNameRegex);
> if (projectNameMatch.Success)
> {
>
> listOfProjects.Add(projectNameMatch.Groups["PROJECTRELATIVEPATH"].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(".csproj") parse parse etc..., however
your method is much more elegant, thanks very much
Apr 26 '06 #3

Post your reply

Help answer this question



Didn't find the answer to your C# / C Sharp question?

You can also browse similar questions: C# / C Sharp