如何以编程方式在当前打开的 Visual Studio IDE 中打开 .sln 文件? [英] How to open a .sln file in the current open Visual Studio IDE programmatically?

查看:88
本文介绍了如何以编程方式在当前打开的 Visual Studio IDE 中打开 .sln 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个插件,可以显示特定目录中的所有文件/文件夹.我想要做的是,如果我在目录中看到一个 .sln 文件,双击想在当前打开的 Visual Studio 解决方案资源管理器中打开解决方案.我使用的是 Visual Studio 2015.

I have developed an add-in that displays all the files/folders in a particular directory. What i would like to do is, if i see a .sln file in the directory, on double click would like to open the solution in the current open Visual Studio solution explorer. I am using Visual Studio 2015.

System.Type type = Type.GetTypeFromProgID("VisualStudio.DTE.14.0");
EnvDTE.DTE dte = (EnvDTE.DTE)System.Activator.CreateInstance(type); 
dte.MainWindow.Visible = true;
dte.Solution.Open(path); 

此特定代码在新的 Visual Studio 中打开解决方案,而不是当前的.

This particular code opens the solution in a fresh Visual Studio and not the current.

非常感谢.

推荐答案

根据这篇文章,最好得到以这种方式当前的解决方案:

According to this post, its better to get current solution this way:

获得DTE的正确方法很简单.实际上,您的加载项已经引用了它在其中运行的 DTE(即溶液被打开).它存储在一个全局变量中_applicationObject 在您的外接程序连接类中.它在您的加载项在 OnConnection 事件处理程序中启动时设置.

The correct way to get DTE is very simple. In fact, your add-in already has reference to DTE in which it runs (that is, in which the solution is opened). It is stored in a global variable _applicationObject in your add-in connect class. It is set when your add-in starts in the OnConnection event handler.

所以,我们可以运行这个来在当前的 Visual Studio 实例中打开一个解决方案:

So, we can run this to open a solution inside current Visual Studio instance:

_applicationObject.Solution.Open(@"D:\folder1\tets.sln");

该代码通常会在您的 Add-Ins Exec 方法中调用,如下所示:

The code will usually called inside your Add-Ins Exec method as following:

public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
    handled = false;
    if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
    {
        if (commandName == "MyAddin1.Connect.MyAddin1")
        {
            _applicationObject.Solution.Open(@"D:\...\tets.sln"); // *** open solution inside current VS instance

            handled = true;
            return;
        }
    }
}

这篇关于如何以编程方式在当前打开的 Visual Studio IDE 中打开 .sln 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆