Open VS 2005 Solution File (.sln) in memory

I would like to open an existing .sln file in memory.

Example of a non-working method:

private Solution2 OpenSolution(string filePath) { Solution2 sln; sln.Open(filePath); return sln; } 

If I have an instance of Solution2, I can call the Open method; but how can I get an instance of Solution2 ?

My goal is to get an adequate project and read some of its settings ... but it's easy, having access to the solution.

+2
source share
4 answers

You can programmatically create a hidden instance of Visual Studio, and then use it to manage your solution. This example lists all the projects that live in this solution.

 using System; using System.Runtime.InteropServices; using EnvDTE; using EnvDTE80; namespace so_sln { class Program { [STAThread] static void Main(string[] args) { System.Type t = System.Type.GetTypeFromProgID("VisualStudio.DTE.8.0", true); DTE2 dte = (EnvDTE80.DTE2)System.Activator.CreateInstance(t, true); // See http://msdn.microsoft.com/en-us/library/ms228772.aspx for the // code for MessageFilter - just paste it into the so_sln namespace. MessageFilter.Register(); dte.Solution.Open(@"C:\path\to\my.sln"); foreach (Project project in dte.Solution.Projects) { Console.WriteLine(project.Name); } dte.Quit(); } } public class MessageFilter : IOleMessageFilter { ... Continues at http://msdn.microsoft.com/en-us/library/ms228772.aspx 

(Stupidity with STAThread and MessageFilter "because of problems with thread conflicts between external multithreaded applications and Visual Studio", whatever that means. Paste code from http://msdn.microsoft.com/en-us/library/ms228772. aspx makes it work.)

+3
source

Solution2 is an interface, not a class. You cannot directly create an object of type Solution2, but only reference objects like Solution2 that contain the Solution2 interface.

As far as I know, the classes that implement the Solution2 interface are only available as part of the collection of interfaces in Visual Studio integration, so you will need to do something similar to what RichieHindle mentions and create a new hidden Visual Studio instance to load the solution.

If you just want to take a couple of settings from the sln file, I would recommend it myself, the file format is pretty simple. If you are trying to pull out the setup, most likely an odd-edge case where the parsing of sln will not work, will also not work with what you are trying to do if Visual Studio parsed sln for you.

+1
source

I don't have much experience with this, but try this msdn article. This is not what you are looking for, but they instantiate the solution2 object in the sample code.

0
source

Solution2 and others are mainly components of the Visual Studio SDK that you will have to redistribute with your application (with all the consequences of licensing).

Since .sln files are plain old XML, you can always open it in an XmlDocument and then XPath into it.

-1
source

Source: https://habr.com/ru/post/947106/


All Articles