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

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

问题描述

我创建在Visual Studio 2013的一个项目的自动化工具,在那里我有我自己的项目模板,我想正在使用一个控制台应用程序下面的代码将其添加到现有的解决方案programatically.I。

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

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

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