none
Adding a file to a Solution Items folder with a custom Project Template Wizard for vs2010

    Question

  • Dearest All,

    I'm trying to create a template for a Test project. I want this template to add several items (e.g. a vsmdi file) to the Solution Items folder. I've created a Template Wizard, and was able to create the Solution Items folder, but I cannot add a file to it. How do I do that?

    Thanks in advance

    Saturday, March 17, 2012 11:34 AM

Answers

  • Hi Artem,

    Unfortunately, the Test team didn't make it very easy to do this. If you unzip the existing template  at C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplates\CSharp\Test\1033\TestProject.zip, you can see it only contains the following:

       assemblyinfo.cs
       TestProject.csproj
       TestProject.vstemplate

    The TestProject.vstemplate makes use of a wizard extension "Microsoft.VisualStudio.TestTools.TestProjectWizards.TestProjectWizardExtension" which is responsible for adding all the other files. I've debugged through this a bit and found the following, which may be of some use to you:

    The .testsettings files are copied from the directory stored as the Template value under the "HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\10.0Exp_Config\EnterpriseTools\QualityTools\RunConfig" registry key. If you navigate to the directory stored in that Template value (typically something like "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Testing Tools\TestProjectCommon\Templates\TestSettings") you'll see where the .testsettings files come from.

    The .vsmdi is also generated programatically, via a call to : Microsoft.VisualStudio.QualityTools.TMI.dll!Microsoft.VisualStudio.TestTools.TestManagement.Tmi.SaveCategories(string location, Microsoft.VisualStudio.TestTools.TestManagement.Filter filter)

    This is a public static method so I believe you can just invoke this directly from your own wizard as needed.

    All these files are added to the solutionitems project by first retrieving the IVsProject3 interface on the SolutionItems Project, then calling AddItemWithSpecific.

    But another option you may wish to consider, is to leverage the same wizard in your own template to add the testsettings and vsmdi files. If your template requires a custom wizard, you think you can chain them by including multiple WizardExtension sections in your vstemplate file.

    It'll take some effort, but I'll see if I can write up a sample of this when I have a few hours to spare.

    Sincerely,


    Ed Dore

    Tuesday, March 20, 2012 7:39 PM

All replies

  • Hi Artem,

    Generally speaking, the idea to do the post project creation is to implatment ProjectFinishedGenerating, ProjectItemFinishedGenerating and RunFinished methods of the IWizard and use DTE object (http://msdn.microsoft.com/en-us/library/envdte.dte.aspx) to do the visual studio automation.

    Once you create the solution item, IDE will load it as an project item. You can enmu all the projects under the solution and to find a project (normally the last one) with name "Solution Items". Next you can call project.projectitems.addfromtemplate method to create the files from the existing item template.

    If I misunderstand you, please let me know.

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, March 19, 2012 6:33 AM
  • Thank you very much for your answer,

    Yes, I'm using the ProjectFinishedGenerating method

    I'm able to create the Solution Items project. However, I cannot add an item to it using the AddFromFile method. I haven't used the AddFromTemplate method though. Can you provide a sample usage? Should the end user have the corresponding template installed?

    Monday, March 19, 2012 3:45 PM
  • Hi Artem,

    Since I didn't find a system preinstalled item template for creating the vsmdi, the above solution my not work for you.

    I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience.Thank you for your understanding and support.

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, March 20, 2012 7:18 AM
  • Hi Artem,

    Unfortunately, the Test team didn't make it very easy to do this. If you unzip the existing template  at C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplates\CSharp\Test\1033\TestProject.zip, you can see it only contains the following:

       assemblyinfo.cs
       TestProject.csproj
       TestProject.vstemplate

    The TestProject.vstemplate makes use of a wizard extension "Microsoft.VisualStudio.TestTools.TestProjectWizards.TestProjectWizardExtension" which is responsible for adding all the other files. I've debugged through this a bit and found the following, which may be of some use to you:

    The .testsettings files are copied from the directory stored as the Template value under the "HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\10.0Exp_Config\EnterpriseTools\QualityTools\RunConfig" registry key. If you navigate to the directory stored in that Template value (typically something like "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Testing Tools\TestProjectCommon\Templates\TestSettings") you'll see where the .testsettings files come from.

    The .vsmdi is also generated programatically, via a call to : Microsoft.VisualStudio.QualityTools.TMI.dll!Microsoft.VisualStudio.TestTools.TestManagement.Tmi.SaveCategories(string location, Microsoft.VisualStudio.TestTools.TestManagement.Filter filter)

    This is a public static method so I believe you can just invoke this directly from your own wizard as needed.

    All these files are added to the solutionitems project by first retrieving the IVsProject3 interface on the SolutionItems Project, then calling AddItemWithSpecific.

    But another option you may wish to consider, is to leverage the same wizard in your own template to add the testsettings and vsmdi files. If your template requires a custom wizard, you think you can chain them by including multiple WizardExtension sections in your vstemplate file.

    It'll take some effort, but I'll see if I can write up a sample of this when I have a few hours to spare.

    Sincerely,


    Ed Dore

    Tuesday, March 20, 2012 7:39 PM
  • Thanks a lot for your answer. I've been busy with other things, but I'll definitely try your approach! I'll update this thread after I manage to implement one of your advices.
    Friday, April 20, 2012 6:12 AM
  • Hey all,

    Does anyone have a working solution already and would it be possible to provide a code sample? I'm having troubles connecting the dots...

    grtz,

    Octavie


    w: http://blog.octavie.nl | t: @eivatco | c: http://mavention.codeplex.com | c: http://www.mavention.nl

    Tuesday, August 14, 2012 2:14 PM
    • Jack, when ever I am trying to create a new test project, I am getting the error "The assemblyinfo.cs" could not be found with in the project templates. How to fix this issue? Screenshot attached.

    Thursday, August 23, 2012 6:42 AM
  • private void AddTestSettingsFile() {
    	const string fileName = "Local.testsettings";
    	var testSettingsFolder = this.GetTestSettingsFolder(this._dte.Version);
    	var sourcePath = Directory.GetFiles(testSettingsFolderfileNameSearchOption.AllDirectories).FirstOrDefault();
    	var destFolder = Directory.GetParent(_replacementsDictionary["$destinationdirectory$"]);
    	var destPath = Path.Combine(destFolder.FullNamefileName);
    	File.Copy(sourcePathdestPathtrue);
    	GetSolutionFolderProject(_solution).ProjectItems.AddFromFile(destPath);
    }
     
    private string GetTestSettingsFolder(string version) {
    	var keyName = string.Format(@"Software\Microsoft\VisualStudio\{0}_Config\EnterpriseTools\QualityTools\RunConfig",
    			                    version);
    	using (var key = Registry.CurrentUser.OpenSubKey(keyName)) {
    		return key.GetValue("TemplateFolder").ToString();
    	}			
    }

    • Edited by Artëm Smirnov Saturday, March 02, 2013 8:30 AM formatting
    Saturday, March 02, 2013 8:29 AM