打开内存中的 VS 2005 解决方案文件 (.sln)

发布于 2024-07-27 21:08:06 字数 319 浏览 2 评论 0原文

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

非工作方法的示例:

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

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

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

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;
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜访吸血鬼 2024-08-03 21:08:06

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

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 使其工作。)

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

﹏半生如梦愿梦如真 2024-08-03 21:08:06

Solution2 是一个接口,而不是一个类。 您不能直接创建 Solution2 类型的对象,只能引用包含 Solution2 接口的 Solution2 对象。

据我所知,实现 Solution2 接口的类仅作为 Visual Studio 集成中接口集合的一部分提供,因此您必须执行类似于 RichieHindle 提到的操作,并创建一个新的隐藏 Visual Studio 实例加载解决方案。

如果您只是想从 sln 文件中获取一些设置,我可能会建议您自己解析它,文件格式非常简单。 如果您试图拉出一个设置,很可能会出现奇怪的边缘情况,即您自己解析 sln 不起作用,如果 Visual Studio 为您解析了 sln,也不适用于您尝试执行的操作。

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.

狂之美人 2024-08-03 21:08:06

我对此没有太多经验,但请尝试 这个 msdn文章。 它不是您直接寻找的东西,但它们确实在示例代码中实例化了solution2对象。

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.

冷…雨湿花 2024-08-03 21:08:06

Solution2 等基本上是 Visual Studio SDK 的一部分,您必须与您的应用程序一起重新分发它们(具有所有许可含义)。

由于 .sln 文件是普通的旧式 XML,因此您始终可以在 XmlDocument 中打开它,然后使用 XPath 打开它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文