Open a VS 2005 Solution File (.sln) into memory

旧时模样 提交于 2019-11-29 15:43:54

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.)

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

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

If you are just wanting to grab a couple settings out of the sln file, I'd potentially recommend parsing it yourself, the file format is pretty simple. If you are trying to pull a setting out, chances are the odd edge case where parsing the sln yourself wouldn't work also wouldn't work with what you are trying to do if Visual Studio parsed the sln for you.

I don't have much experience with this, but try this msdn article. It isn't directly what you are looking for but they do instantiate a solution2 object in the sample code.

Solution2 et al are basically parts of Visual Studio SDK, which you'd have to redistribute with your application (with all the licensing implications).

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!