11

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?

Yurii
  • 4,811
  • 7
  • 32
  • 41
user3114639
  • 1,895
  • 16
  • 42
  • **I need to perform C# code** what u mean by this? can you please explain in details. Do you mean you want to run C# cmd/u want to insert predefine C# code in project/you wan to trigger some C# exe or what? – Deepak Sharma Apr 23 '14 at 12:00
  • I have C# Code which using EnvDTE etc, and I want to add this as function or something else that will be called when the project is created. – user3114639 Apr 23 '14 at 12:06
  • I am not sure, but try if this can help u out. [click this link](http://www.codeproject.com/Articles/36219/Exploring-EnvDTE) – Deepak Sharma Apr 23 '14 at 12:54

4 Answers4

1

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;
Yurii
  • 4,811
  • 7
  • 32
  • 41
0

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

MateusBello
  • 376
  • 3
  • 8
  • I tried it before, but VS2010 C++ template project supports just javascript code, and I want to perform C# code. – user3114639 Apr 29 '14 at 09:47
0

If you are trying to catch an interactive project creation (i.e. 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.

Joe Steele
  • 563
  • 5
  • 13
0

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

Roytazz
  • 1
  • 1
  • That way can work theoretical, but there are 3 problems: One, I have just VS Add-In, I can't checking the size of user's folder all the time (Which event can I perform that?) . The second one- the user can add his project in any location in his computer, so I don't have a specific folder to check. The third, when I detect size changing, how can I know which project added? and maybe the user added another files? – user3114639 May 12 '14 at 16:43