How can I parse Visual Studio solution (SLN) files in .NET? I would like to write an app that merges multiple solutions into one while saving the relative build order.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- Correctly parse PDF paragraphs with Python
JetBrains (the creators of Resharper) have public sln parsing abilities in their assemblies (no reflection needed). It's probably more robust than the existing open source solutions suggested here (let alone the ReGex hacks). All you need to do is:
JetBrains.Platform.ProjectModel
JetBrains.Platform.Util
JetBrains.Platform.Interop.WinApi
The library is not documented, but Reflector (or indeed, dotPeek) is your friend. For example:
Everything is great, but I wanted also to get sln generation capability - in code snapshot above you're only parsing .sln files - I wanted to make similar thing except to be able to re-generate sln with slight modifications back to .sln file. Such cases could be for example porting same project for different .NET platform. For now it's only sln re-generation, but later on I will expand it to projects as well.
I guess that I wanted also to demonstrate the power of regular expressions and native interfaces. (Smaller amount of code with more functionality)
Update 4.1.2017 I've created separate svn repository for parsing .sln solution: https://sourceforge.net/p/syncproj/code/HEAD/tree/
Below is my own code sample snippet (predecessor). You're free to use any of them.
It's possible that in future svn based solution parsing code will be update with generation capabilities as well.Update 4.2.2017 Source code in SVN is supporting .sln generation as well.
With Visual Studio 2015 there is now a publicly accessible
SolutionFile
class which can be used to parse solution files:This class is found in the Microsoft.Build.dll 14.0.0.0 assembly. In my case it was located at:
Thanks to Phil for pointing this out!
I can't really offer you a library and my guess is there isn't one that exists out there. But I've spent a deal of time messing around with .sln files in batch editting scenarios and I've found Powershell to be a very useful tool for this task. The .SLN format is pretty simple and can be almost completely parsed with a few quick and dirty expressions. For Example
Included Project files.
It's not always pretty, but it is an effective way to do batch processing.
Thank @John Leidegren he offers an effective way. I write a hlper class for I cant use his code that cant find the
s_SolutionParser_configurations
and the projects without FullName.The code is in github that can get the projects with the FullName.
And the code cant get SolutionConfiguration.
But when you dev a vsx the vs will say cant find
Microsoft.Build.dll
,so you may try use dte to get all the projects.The code that use dte to get all the projects is in github
See : http://www.wwwlicious.com/2011/03/29/envdte-getting-all-projects-html/
The .NET 4.0 version of the Microsoft.Build assembly contains a SolutionParser class in the Microsoft.Build.Construction namespace that parses Visual Studio solution files.
Unfortunately this class is internal, but I've wrapped some of that functionality in a class that uses reflection to get at some common properties you might find helpful.
Note that you have to change your target framework to ".NET Framework 4" (not client profile) to be able to add the Microsoft.Build reference to your project.