在 Visual Studio 扩展中,如何响应 "Open Folder" 以及 "Open Solution"?

In a Visual Studio Extension, how to respond to "Open Folder" as well as "Open Solution"?

我的 Visual Studio 扩展通过 IVsSolutionEvents.OnAfterOpenSolution() 响应解决方案的打开。

Visual Studio 2017 引入了 "Open Folder" 作为 "Open Solution" 的替代方法,但是当您打开文件夹时,IVsSolutionEvents.OnAfterOpenSolution() 不会触发。 (也没有 IVsSolutionEvents 中的任何其他事件,也没有 IVsSolutionLoadEvents 中的任何事件。)

我的扩展程序如何知道何时打开文件夹而不是解决方案?

您必须使用为 Visual Studio 2017 添加的 IVsSolutionEvents7.OnAfterOpenFolder Method

Notifies listening clients that the folder has been opened.

public void OnAfterOpenFolder (string folderPath);

由于这是本机 COM 接口,您还必须确保实现 class 是 COM 可见的(通过您可以在程序集上设置的 ComVisible 属性,在 class,基于 class,等等)。