Parsing Visual Studio Solution Files

Parse a .Net 4 Solution File

Solution files are text based rather than XML. There is information on their format here Solution (.sln) File.

Most project files (e.g. .csproj files) are in the MSBuild file format which is XML based. A standard XML parser can be used to read these files and the file format is documented here MSBuild Reference.

Visual Studio solution parser

I've posted an answer to this question which you might find helpful. I'm using the SolutionParser, an internal class of the Microsoft.Build framework, which handles solution files.

Library for parsing Visual Studio Solution files

Get list of projects from Visual Studio .sln file using Microsoft.Build namespace

For parsing solution file you can use SolutionFile class:

var solutionFile = SolutionFile.Parse(@"SOLUTION_PATH.sln");
var projectNames = solutionFile.ProjectsInOrder.Select(p => p.ProjectName).ToList();

Visual Studio Online Error parsing solution file at *.xproj

I found the answer to this problem and it is due to VSO automatically doing a NuGet restore. After I unchecked NuGet restore it works fine. In the tutorial there is a step that adds 'dnu restore' which is where the packages are being restored.

Parse ProjectDependency from .sln file

        var filePath = @"C:\Users\xyz\Documents\Visual Studio 2010\Projects\test\test.sln"
var slnLines = File.ReadAllLines(filePath);
var projGuid = new List<string>();
for(var i=0; i<slnLines.Length; i++)
{
if (slnLines[i].Contains(@"ProjectSection(ProjectDependencies)"))
while (!slnLines[i+1].Contains("EndProjectSection"))
{
var temp = (slnLines[i+1].Split(new[]{'=', '\n', '\t', ' '},2,StringSplitOptions.RemoveEmptyEntries));
if(temp.ElementAt(0) == temp.ElementAt(1))
projGuid.Add(temp.ElementAt(0));
i++;
}
}

using (var file = new StreamWriter(@"C:\Users\xyz\Desktop\Results.txt"))
{
foreach (var pGuid in projGuid)
{
file.WriteLine(pGuid);
}
}

This piece of code does it.

Thanks.

Visual Studio, Copy Solution to Folder On Build

You can do this with a batch file using

xcopy /D

which will copy only those files whose source time is newer than the destination time. Or robocopy.

This could recursively copy the entire directory, particular folders, or particular file types. If you're willing to launch your builds from the batch file itself (rather than within visual studio) the batch file has everything you need. Otherwise you might find Solution-wide pre-build event? useful for setting this up to work through Visual Studio. If you follow that process you should be able to take advantage of the Visual Studio Macros for Build Commands like $(SolutionPath).

If you need only those files actually contained in your projects then parsing the solution and project files may be necessary. The .NET framework provides some libraries to help parse solution or project files (SolutionParser). See
Parsing Visual Studio Solution files

That would be a bit of work, but you build a list of only those files contained in your projects and use with xcopy or robocopy.

How can I use an SLN file with MSBuild programmatically from C#?

I figured it out after more searching. Finding good examples online is a little difficult because of the two different versions of the MSBuild API, and the popularity of just running MSBuild from the command line.

Here is the code that is now working for me, using the newer MSBuild API:

var pc = new ProjectCollection();

var parameters = new BuildParameters(pc)
{
Loggers = new[] { _logger } //Instance of ILogger instantiated earlier
};

var request = new BuildRequestData(
projectFullPath: pathToMySlnFile, //Solution file path
globalProperties: myPropertyDictionary,
toolsVersion: null,
targetsToBuild: myTargetsArray,
hostServices: null,
flags: BuildRequestDataFlags.ProvideProjectStateAfterBuild);

var buildResult = BuildManager.DefaultBuildManager.Build(parameters, request);


Related Topics



Leave a reply



Submit