In Visual Studio 2012 (or below, preferably), is it possible to direct a newly created item to a particular folder in the filesystem instead of it going directly to the project root with a project item template?

The ProjectItem.Save() and SaveAs() methods throw an unimplemented exception.

有帮助吗?

解决方案

The short answer is no: you can't direct a file to a subfolder of your project using a vstemplate alone.

If you don't mind writing a VS extension, you could try this approach which will create the file in the desired subfolder by replacing the "Add New Item" command in Solution Explorer with your own implementation:

  1. Override the "Add New Item" command with your own event handler (Command GUID: {5EFC7975-14BC-11CF-9B2B-00AA00573819}, ID: 220) http://social.msdn.microsoft.com/Forums/eu/vsx/thread/c0e7de35-73d7-441a-979d-f5e359896b80)
  2. In your event handler, pre-select the subfolder in the Solution Explorer Hierarchy (see http://social.msdn.microsoft.com/Forums/en/vsx/thread/0bef0d6e-c416-4e09-989f-5c2da7379e04)
  3. Invoke the IVsAddProjectItemDlg.AddProjectItemDlg method, optionally specifying your project item template as the default selected item (see http://social.msdn.microsoft.com/Forums/en/vsx/thread/f24dd433-1522-47ad-bd02-067b13391527)

The downside to this approach is that every new project item will be added to the project subfolder. To avoid this, you could add a new context menu command to the Solution Explorer instead of overriding "Add New Item".

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top