Click here to Skip to main content
15,896,207 members
Articles / Programming Languages / C#

LINQ to Visual Studio Solution

Rate me:
Please Sign up or sign in to vote.
4.20/5 (7 votes)
1 Dec 2009CPOL3 min read 28.6K   224   16  
Search properties of projects within a solution using LINQ.
/*
 * TAGS:  Parse, Solution File, sln
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace LinqToVisualStudioSolution
{
    public class Solution : List<Project>
    {
        /// <summary>
        /// Keep a list of errors instead of halting execution flow.
        /// </summary>
        public List<string> Errors { get; private set; }

        public string SolutionPath { get; private set; }
        //public List<Project> Projects { get; private set; }

        /// <summary>
        /// Creates a List of Project objects from the projects within the solution.
        /// Limitations: If any projects withing the solution are not C# projects, they will be skipped.
        /// </summary>
        /// <param name="solutionPath">Path to the solution to load.</param>
        public Solution(string solutionPath) : base()
        {
            this.Errors = new List<string>();

            this.SolutionPath = solutionPath;
            //this.Projects = new List<Project>();

            string[] solutionAllLines = File.ReadAllLines(solutionPath);

            // Only bother with the lines that specify what projects are in the solution.

            var solutionProjectLines = from p in solutionAllLines
                                       where p.StartsWith("Project")
                                       select p;
            
            // Fill the List


            foreach (var projectLine in solutionProjectLines)
            {
                Environment.CurrentDirectory = Path.GetDirectoryName(solutionPath);

                string pathToProject = Regex.Match(projectLine, @", ""(.+\...proj)").Groups[1].Value;
                string guid = Regex.Match(projectLine, @", ""(\{.+\})").Groups[1].Value;
                string projectName = Regex.Match(projectLine, @"= \""(\w+)\""").Groups[1].Value;

                if (pathToProject.Length == 0)
                {
                    this.Errors.Add(solutionPath + " contains a project without a valid path:" + projectName);
                    continue;
                }

                if (!pathToProject.StartsWith("http:"))
                {
                    pathToProject = Path.GetFullPath(pathToProject);
                }

                Project pr = new Project(
                    guid,
                    projectName,
                    pathToProject);

                this.Add(pr);
            }
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(this.SolutionPath);
            foreach (var err in this.Errors)
            {
                sb.Append("Error:");
                sb.AppendLine(err);
            }
            sb.AppendLine(":");
            foreach (var project in this)
            {
                sb.AppendLine(project.ToString());
            }

            return sb.ToString();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Other
South Africa South Africa
Joel Williams is a professional software developer turned preacher. Since he moved to South Africa, he has been out of the software industry for several years, but he tries to keep his skills sharp through hobby programming.

While developing professionally, Joel mostly used VB.NET and ASP.NET, but he now prefers C# and Windows Forms or Windows Mobile development.

Comments and Discussions