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?
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.
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.
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.
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:
Probably the most difficult part was to find the right guid
and commandId
, but this article was of help.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With