Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event on Visual Studio project creation

I want to add functionality when the user creates project \ solution in Visual Studio 2010\2012. i.e. I need to perform C# code when a new project is created.

I googled a lot but didn't find any event which is fired on\after project creation.
Is there any way to do that?

like image 673
user3114639 Avatar asked Apr 23 '14 11:04

user3114639


People also ask

How do I create an event in Visual Studio?

To create an event handler in Visual BasicFrom the Class Name drop-down list at the top of the Code Editor, select the object that you want to create an event handler for. From the Method Name drop-down list at the top of the Code Editor, select the event.

What is an event in Visual Studio?

An event is a signal that informs an application that something important has occurred. For example, when a user clicks a control on a form, the form can raise a Click event and call a procedure that handles the event.

How do I view events in Visual Studio?

Select Alt+F2 to open the Performance Profiler in Visual Studio. Select the Events Viewer check box. Select the Start button to run the tool. After the tool starts running, go through the scenario to profile in your app.


Video Answer


1 Answers

After some research and thanks to article mentioned by @Joe Steele, I've managed to hook up the CommandEvents.AfterExecute event, responsible for project creation:

DTE application = this.GetService(typeof(SDTE)) as DTE;
string guidVSStd97 = "{5efc7975-14bc-11cf-9b2b-00aa00573819}".ToUpper();
int cmdidNewProject = 216;

this.createCmd = application.Events.CommandEvents[guidVSStd97, cmdidNewProject];
this.createCmd.AfterExecute += this.command_AfterExecute;

And this is how the delegate looks like:

void command_AfterExecute(string Guid, int ID, object CustomIn, object CustomOut)
{
    // place your code here
}

Notes:

  1. Probably the most difficult part was to find the right guid and commandId, but this article was of help.

  2. Sometimes, during debugging, event didn't fire at all, which made me baffled, but thankfully I've stumbled across this piece of advise:

The SolutionEvents object can go out of scope and be garbage collected before the solution is closed. To retain a reference to this object, declare a private variable in the class in which you implement the solution event handlers.Source

Thus introduction of private variable solved the issue:

private CommandEvents createCmd;
like image 165
Yurii Avatar answered Oct 02 '22 08:10

Yurii