在 Visual Studio IDE 之外获取 EnvDTE.DTE 实例 [英] Getting EnvDTE.DTE instance outside Visual Studio IDE

查看:16
本文介绍了在 Visual Studio IDE 之外获取 EnvDTE.DTE 实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Visual Studio 2013 中创建一个项目自动化工具,我在其中拥有自己的项目模板,并尝试以编程方式将其添加到现有解决方案中.我在控制台应用程序中使用以下代码.

I am creating a project automation tool in Visual Studio 2013 where I have my own project template and I am trying to add it to an existing solution programatically.I am using the following code in a console application.

EnvDTE.DTE dte = (EnvDTE.DTE)Marshal.GetActiveObject("VisualStudio.DTE.12.0");
string solDir = dte.Solution.FullName;
solDir=solDir.Substring(0, solDir.LastIndexOf("\"));
dte.Solution.AddFromTemplate(path, solDir+"\TestProj", "TestProj", false);

当我从 Visual Studio IDE 运行应用程序时它正在工作.但是当我尝试从命令提示符运行 exe 时,出现以下异常.

It is working when I run the application from Visual Studio IDE. But when I try to run the exe from command prompt, I get the following exception.

Unhandled Exception: System.Runtime.InteropServices.COMException: Operation unav
ailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))
at System.Runtime.InteropServices.Marshal.GetActiveObject(Guid& rclsid, IntPtr reserved, Object&   ppunk)
at System.Runtime.InteropServices.Marshal.GetActiveObject(String progID)
at ProjectAutomation.Console.Program.Main(String[] args) 

我想知道是否有任何方法可以在 Visual Studio IDE 之外获取活动的 EnvDTE.DTE 实例.?

I want to know whether there is any way to get the active EnvDTE.DTE instance outside Visual Studio IDE .?

推荐答案

从外部工具自动化现有的 Visual Studio 实例以修改加载的解决方案是一个坏主意.如果您使用 GetActiveObject(...) 并且启动了两个 Visual Studio 实例,您如何知道返回了正确的实例?当用户启动外部工具时,如果用户或 Visual Studio 正在对解决方案做些什么呢?有两种更好的方法:

Automating an existing Visual Studio instance from an external tool to modify a loaded solution is a bad idea. If you use GetActiveObject(...) and there are two Visual Studio instances launched, how do you know that the correct instance is returned? And what if the user or Visual Studio is doing something with the solution when the user launches the external tool? There are two better approaches:

1) 使用外部工具自动化一个新的 Visual Studio 实例,加载所需的解决方案并修改它.即使 VS 实例不可见,也可以这样做.要创建一个新实例,正确的代码是:

1) Use an external tool to automate a new Visual Studio instance, load the desired solution and modify it. This can be done even with the VS instance not visible. To create a new instance the proper code is:

System.Type type = Type.GetTypeFromProgID("VisualStudio.DTE.12.0");
EnvDTE.DTE dte = (EnvDTE.DTE) System.Activator.CreateInstance(type);
dte.MainWindow.Visible = true;
...

2) 使用 Visual Studio 扩展,例如宏(VS 2010 或更低版本)、加载项(VS 2013 或更低版本)或包(任何 VS 版本)来提供菜单项或按钮工具栏,单击时, 修改当前加载的解决方案.这可以防止忙"的情况,因为如果 VS 忙,则无法单击菜单项或工具栏按钮(除非忙"操作是异步的).

2) Use a Visual Studio extension, such as a macro (VS 2010 or lower), add-in (VS 2013 or lower) or package (any VS version) to provide a menu item or button toolbar that, when clicked, modifies the currently loaded solution. This prevent the "busy" scenario because if VS is busy the menu item or toolbar button can't be clicked (unless the "busy" operation is asynchronous).

这篇关于在 Visual Studio IDE 之外获取 EnvDTE.DTE 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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