I would like to open into memory an existing .sln file.
Example of a non-working method:
private Solution2 OpenSolution(string filePath)
{
Soluti
You can programmatically create a hidden instance of Visual Studio, and then use it to manipulate your solution. This example will list out all the projects that live in the given 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
(The nonsense with STAThread and MessageFilter is "due to threading contention issues between external multi-threaded applications and Visual Studio", whatever that means. Pasting in the code from http://msdn.microsoft.com/en-us/library/ms228772.aspx makes it work.)