简体   繁体   中英

Event on Visual Studio project creation

I want to add functionality when the user creates project \\ solution in Visual Studio 2010\\2012. ie 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?

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;

Create a template project that has your code in it. So other users can use your template instead of the default VS templates.

If you are trying to catch an interactive project creation (ie the user selected "New -> Project" from the menu) you should be able to just hook the menu event. However Visual Studio doesn't provide those events directly.

This post from Microsoft support talks about getting around that . Yes -- the article is very old, but I believe is still accurate. Please correct me if I am wrong. It has been a long time since I created a VS extension. The event friendly name is probably something like "New.Project". You will have to figure out the GUID and ID.

If you have not created a Visual Studio extension before, this is a good place to start .

If you are trying to catch non-interactive project creation -- there are a lot more places to hook. Someone wiser than I will have to chime in.

The problem is that events are only fired at runtime. And since the creation of a project doesn't happen in runtime there are no events for this.

The only way I can think of is checking the size of the folder containing all projects and see if it changed, then fire your C# code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM