通过插件从编程角度获取当前Visual Studio IDE解决方案目录

21

我有一些工具可以对.NET解决方案进行更新,但它们需要知道解决方案所在的目录。

我将这些工具添加为外部工具,它们将出现在IDE工具菜单中,并提供$(SolutionDir)作为参数。这很有效。

然而,我希望用户能够通过自定义顶级菜单(我创建了一个Visual Studio集成包项目)和通过解决方案节点上的上下文菜单(我创建了一个Visual Studio插件项目)更轻松地访问这些工具。我正在寻找一种方法来从这些上下文中获取当前解决方案目录。

我尝试从VisualStudio.DTE对象获取解决方案信息:

EnvDTE.DTE dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE");
string solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);

但是,这返回的是插件的解决方案目录,而不是当前解决方案的目录。

我尝试回显$(SolutionDir)并读取它:

System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "echo $(SolutionDir)");

// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();

但是,这返回的是IDE的目录,而不是当前解决方案的目录。

在解决方案节点CommandBar中,我没有看到任何相关信息。

或者,如果有一种可以以编程方式访问已定义的Visual Studio外部工具并启动它们(使用已定义的宏参数)的方法,则可以使用该方法。

解决方案是什么?


再次加2,显然我在这里跟踪你,带着这个DTE疯狂的东西。 - Terrance
2个回答

18

EnvDTE.DTE dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE"); string solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);

不过,这段代码返回的是附加组件的解决方案目录,而不是当前解决方案的目录。

获取目录的方法是正确的,问题在于获取 VisualStudio.DTE 对象的方式。这段代码在哪里被调用了呢?我猜它被调用在你的附加组件中。你是否在 Visual Studio 中执行(调试)你的附加组件,从而打开了另一个实例的 Visual Studio,在其中打开了你的解决方案?所以你有两个 Visual Studio 实例。

GetActiveObject("VisualStudio.DTE") 获取的是一个随机的 Visual Studio 实例。在你的情况下,显然是带有附加组件项目的 Visual Studio,因为你得到了指向你附加组件的路径。这就是解释你的问题原因。

获取 DTE 的正确方式非常简单。事实上,你的附加组件已经有了 DTE 的引用,它运行在其中(也就是打开了解决方案的那个)DTE 中。它存储在你的附加组件连接类的全局变量 _applicationObject 中,在你的附加组件启动时,它会在 OnConnection 事件处理程序中设置。所以你只需要调用:

string solutionDir = System.IO.Path.GetDirectoryName(_applicationObject.Solution.FullName);

谢谢Peter,那正是问题和解决方案!现在我会寻找一种方法,使通过自定义菜单执行工具的输出进入输出窗口而不是单独的窗口,这样一切都将完美地运行。再次感谢。 - Dave Clemmer

6

在 Peter 的帮助下,我设置了上下文菜单插件,可以通过解决方案目录启动外部工具,并将结果输出到输出窗格。以下是插件的一些示例说明:

    ///--------------------------------------------------------------------------------
    /// <summary>This method implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
    ///
    /// <param term='application'>Root object of the host application.</param>
    /// <param term='connectMode'>Describes how the Add-in is being loaded.</param>
    /// <param term='addInInst'>Object representing this Add-in.</param>
    /// <seealso class='IDTExtensibility2' />
    ///--------------------------------------------------------------------------------
    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;

        // Get the solution command bar
        CommandBar solutionCommandBar = ((CommandBars)_applicationObject.CommandBars)["Solution"];

        // Set up the main InCode
        CommandBarPopup solutionPopup = (CommandBarPopup)solutionCommandBar.Controls.Add(MsoControlType.msoControlPopup, System.Reflection.Missing.Value, System.Reflection.Missing.Value, 1, true);
        solutionPopup.Caption = "InCode";

        // Add solution updater submenu
        CommandBarControl solutionUpdaterControl = solutionPopup.Controls.Add(MsoControlType.msoControlButton, System.Reflection.Missing.Value, System.Reflection.Missing.Value, 1, true);
        solutionUpdaterControl.Caption = "Update Solution";
        updateSolutionMenuItemHandler = (CommandBarEvents)_applicationObject.Events.get_CommandBarEvents(solutionUpdaterControl);
        updateSolutionMenuItemHandler.Click += new _dispCommandBarControlEvents_ClickEventHandler(updateSolution_Click);
    }

    // The event handlers for the solution submenu items
    CommandBarEvents updateSolutionMenuItemHandler;

    ///--------------------------------------------------------------------------------
    /// <summary>This property gets the solution updater output pane.</summary>
    ///--------------------------------------------------------------------------------
    protected OutputWindowPane _solutionUpdaterPane = null;
    protected OutputWindowPane SolutionUpdaterPane
    {
        get
        {
            if (_solutionUpdaterPane == null)
            {
                OutputWindow outputWindow = _applicationObject.ToolWindows.OutputWindow;
                foreach (OutputWindowPane loopPane in outputWindow.OutputWindowPanes)
                {
                    if (loopPane.Name == "Solution Updater")
                    {
                        _solutionUpdaterPane = loopPane;
                        return _solutionUpdaterPane;
                    }
                }
                _solutionUpdaterPane = outputWindow.OutputWindowPanes.Add("Solution Updater");
            }
            return _solutionUpdaterPane;
        }
    }

    ///--------------------------------------------------------------------------------
    /// <summary>This method handles clicking on the Update Solution submenu.</summary>
    ///
    /// <param term='inputCommandBarControl'>The control that is source of the click.</param>
    /// <param term='handled'>Handled flag.</param>
    /// <param term='cancelDefault'>Cancel default flag.</param>
    ///--------------------------------------------------------------------------------
    protected void updateSolution_Click(object inputCommandBarControl, ref bool handled, ref bool cancelDefault)
    {
        try
        {
            // set up and execute solution updater thread
            UpdateSolutionDelegate updateSolutionDelegate = UpdateSolution;
            updateSolutionDelegate.BeginInvoke(UpdateSolutionCompleted, updateSolutionDelegate);
        }
        catch (System.Exception ex)
        {
            // put exception message in output pane
            SolutionUpdaterPane.OutputString(ex.Message);
        }
    }

    protected delegate void UpdateSolutionDelegate();

    ///--------------------------------------------------------------------------------
    /// <summary>This method launches the solution updater to update the solution.</summary>
    ///--------------------------------------------------------------------------------
    protected void UpdateSolution()
    {
        try
        {
            // set up solution updater process
            string solutionDir = System.IO.Path.GetDirectoryName(_applicationObject.Solution.FullName);
            System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(@"SolutionUpdater.exe", solutionDir);
            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;
            procStartInfo.CreateNoWindow = true;
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo = procStartInfo;

            // execute the solution updater
            proc.Start();

            // put solution updater output to output pane
            SolutionUpdaterPane.OutputString(proc.StandardOutput.ReadToEnd());
            SolutionUpdaterPane.OutputString("Solution update complete.");
        }
        catch (System.Exception ex)
        {
            // put exception message in output pane
            SolutionUpdaterPane.OutputString(ex.Message);
        }
    }

    ///--------------------------------------------------------------------------------
    /// <summary>This method completing the update solution thread.</summary>
    ///
    /// <param name="ar">IAsyncResult.</param>
    ///--------------------------------------------------------------------------------
    protected void UpdateSolutionCompleted(IAsyncResult ar)
    {
        try
        {
            if (ar == null) throw new ArgumentNullException("ar");

            UpdateSolutionDelegate updateSolutionDelegate = ar.AsyncState as UpdateSolutionDelegate;
            Trace.Assert(updateSolutionDelegate != null, "Invalid object type");

            updateSolutionDelegate.EndInvoke(ar);
        }
        catch (System.Exception ex)
        {
            // put exception message in output pane
            SolutionUpdaterPane.OutputString(ex.Message);
        }
    }

没有找到轮询外部进程的方法,最终我在VS包中将所需操作作为内部进程完成。 - Dave Clemmer
我对结果轮询(或者说将输出流到输出窗格)有一个解决方案,至少在使用VSPackage时。这与本问题的范围无关(并且无法适用于此处...),因此您可以提出一个新问题,我会在那里回答。 - Tatu Lahtela
好的,我为此准备了一个单独的问题,如果你的答案看起来不错,我会接受的!https://dev59.com/Tl3Va4cB1Zd3GeqPEdgC - Dave Clemmer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接