NArrange.Core.MSBuildSolutionParser.Parse C# (CSharp) Method

Parse() public method

Parses project file names from a solution file.
public Parse ( string solutionFile ) : ReadOnlyCollection
solutionFile string Solution file name.
return ReadOnlyCollection
        public ReadOnlyCollection<string> Parse(string solutionFile)
        {
            if (solutionFile == null)
            {
                throw new ArgumentNullException("solutionFile");
            }

            string solutionPath = Path.GetDirectoryName(solutionFile);

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

            using (StreamReader reader = new StreamReader(solutionFile, true))
            {
                while (!reader.EndOfStream)
                {
                    //
                    // Find lines like the following:
                    // Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NArrange.Core", "NArrange.Core\NArrange.Core.csproj", "{CD74EA33-223D-4CD9-9028-AADD4E929613}"
                    //
                    string line = reader.ReadLine().TrimStart();
                    if (line.StartsWith("Project(", StringComparison.OrdinalIgnoreCase))
                    {
                        string[] projectData = line.Split(',');
                        string projectFile = projectData[1].Trim().Trim('"');
                        string projectPath = Path.Combine(solutionPath, projectFile);
                        if (!string.IsNullOrEmpty(Path.GetExtension(projectPath)))
                        {
                            projectFiles.Add(projectPath);
                        }
                    }
                }
            }

            return projectFiles.AsReadOnly();
        }

Usage Example

        public void ParseTest()
        {
            string[] testProjectFiles = new string[]
            {
                Path.Combine(Path.GetTempPath(), "TestProject.csproj")
            };

            MSBuildSolutionParser solutionParser = new MSBuildSolutionParser();
            ReadOnlyCollection<string> projectFiles = solutionParser.Parse(_testSolutionFile);

            Assert.AreEqual(testProjectFiles.Length, projectFiles.Count, "Unexpected number of project files.");

            foreach (string testProjectFile in testProjectFiles)
            {
                Assert.IsTrue(
                    projectFiles.Contains(testProjectFile),
                    "Test project file {0} was not included in the project file list.",
                    testProjectFile);
            }
        }
All Usage Examples Of NArrange.Core.MSBuildSolutionParser::Parse
MSBuildSolutionParser