【发布时间】:2014-12-08 11:04:34
【问题描述】:
我正在 Visual Studio 2013 中创建一个项目自动化工具,我在其中拥有自己的项目模板,并尝试以编程方式将其添加到现有解决方案中。我在控制台应用程序中使用以下代码。
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 时,出现以下异常。
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 实例。?
【问题讨论】:
-
Marshal.GetActiveObject("VisualStudio.DTE.12.0") 为我工作。将 exception.ToString() 结果添加到问题中。
-
我在一个普通的控制台应用程序中使用了上面的代码。我应该使用 Visual Studio 中的任何可扩展项目模板吗?
-
我的水晶球说 VS 正在运行,但你的控制台应用程序没有。
-
仅供参考,如果有多个实例在运行,您的代码将无法按预期运行。
标签: c# visual-studio-2013 envdte