Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically run extension code in Visual Studio on startup

Can I create an extension for Visual Studio that runs in the background as soon as the user opens the Visual Studio IDE? For example, I am building an extension that gets the current active file address in Visual Studio (with C#), but I would like this extension to always run in the background without having to be activated by the user clicking a button or pressing some key combination.

Is this possible, and if so, what is the best way of doing it?

Any help would be greatly appreciated! Regards, Erfan

like image 229
Erfan Farhangy Avatar asked Mar 05 '15 15:03

Erfan Farhangy


People also ask

How do I run an extension code in Visual Studio?

Browse for extensions# You can browse and install extensions from within VS Code. Bring up the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of VS Code or the View: Extensions command (Ctrl+Shift+X).

What is VSIX?

A VSIX package is a . vsix file that contains one or more Visual Studio extensions, together with the metadata Visual Studio uses to classify and install the extensions. That metadata is contained in the VSIX manifest and the [Content_Types]. xml file. A VSIX package may also contain one or more Extension.

How do I use VSIX in Visual Studio?

Open in VisualStudio the folder that contain the "nameFile. vsix" file. File, Open Folder..., click right in the "nameFile. vsix" into de VisualStudio, and click in install extension VSIX.


2 Answers

Since you tagged your question with visual-studio-2010 I assume you are working on an "Add-in" rather than a "VSPackage Extensions".

In this case, you can use the OnConnection event handler.

If you are working on a VSPackage Extensions, you can use the attribute ProvideAutoLoad.

Just search for these, you will find sufficient information. Both ways are also described shortly here under "How can I run my add-in code in a VSPackage?"

like image 88
Baris Akar Avatar answered Sep 18 '22 14:09

Baris Akar


For VS 2010 and higher the recommended extensibility approach is a package (VS 2015 won't allow add-ins).

To get the package loaded when Visual Studio is loaded see HOWTO: Autoload a Visual Studio package.

Once loaded, your package may be interested in two different kind of selection change events:

  • To get notified when the selection in the Solution Explorer changes, get the IVsMonitorSelection interface and call the AdviseSelectionEvents/UnadviseSelectionEvents and provide a class that implements the IVsSelectionEvents interface.
  • To get notified when the active window changes (which can be a document window or a toolwindow), implement the IVsWindowFrameNotify interface.
like image 27
Carlos Quintero Avatar answered Sep 21 '22 14:09

Carlos Quintero