Automating Visual Studio instance from separate process

前端 未结 2 1552
抹茶落季
抹茶落季 2020-12-18 10:59

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 t

相关标签:
2条回答
  • 2020-12-18 11:31

    If you are just trying to automate builds/packaging of your apps, you should look into MSBuild -- it is Microsoft's build engine that lets you script automation of most of the functions of Visual Studio.

    0 讨论(0)
  • 2020-12-18 11:33

    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
    

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

    0 讨论(0)
提交回复
热议问题