2005解决方案文件

2005解决方案文件

本文介绍了将VS 2005解决方案文件(.sln)打开到内存中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个现有的.sln文件打开到内存中.

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

不起作用的方法的示例:

Example of a non-working method:

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

如果我有Solution2的实例,则可以调用方法Open;但是如何获取Solution2的实例?

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

然后我的目标是获得足够的项目并阅读其一些设置...但这很容易获得解决方案.

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

推荐答案

您可以以编程方式创建Visual Studio的隐藏实例,然后使用它来操纵您的解决方案.此示例将列出存在于给定解决方案中的所有项目.

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

(使用STAThread和MessageFilter的废话是由于外部多线程应用程序和Visual Studio之间存在线程争用问题",无论如何.粘贴来自 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.)

这篇关于将VS 2005解决方案文件(.sln)打开到内存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 09:12