从单独的进程自动化

从单独的进程自动化

本文介绍了从单独的进程自动化 Visual Studio 实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法编写可以连接到正在运行的 Visual Studio 实例并向其发出命令的应用程序?例如,我是否可以编写一个带有按钮的 WPF 应用程序,当单击该按钮时,向已打开的 Visual Studio 实例发出Build.BuildSolution"命令,使其开始构建?

Is there a way to write an application that can connect to a running instance of Visual Studio and issue commands to it? For example, could I write a WPF app with a button that, when clicked, issues a "Build.BuildSolution" command to an already-open instance of Visual Studio, causing it to start a build?

我确定我可以使用 SendKeys 发送 Ctrl+Shift+B,但我想知道是否有一种方法可以写入实际的 API 来自动化 Visual Studio,并按名称调用命令.

I'm sure I could use SendKeys to send Ctrl+Shift+B, but I want to know if there's a way to write to an actual API to automate Visual Studio, and invoke commands by name.

推荐答案

这是一个连接到正在运行的 Visual Studio 并发出 Build 命令的 C# 程序.DTE.9 部分表示Visual Studio 2008"——VS 2005 使用 DTE.8,VS 2010 使用 DTE.10.

Here's a C# program that connects to a running Visual Studio and issues a Build command. The DTE.9 part means "Visual Studio 2008" - use DTE.8 for VS 2005, or DTE.10 for VS 2010.

using System;
using System.Runtime.InteropServices;
using EnvDTE80;

namespace SORemoteBuild
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            // Get an instance of the currently running Visual Studio IDE.
            EnvDTE80.DTE2 dte2;
            dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.
                                      GetActiveObject("VisualStudio.DTE.9.0");
            dte2.Solution.SolutionBuild.Build(true);
        }
    }

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

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

这篇关于从单独的进程自动化 Visual Studio 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 05:38