Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have the following code which adds a project to the current solution from a specified template:

C#
Solution2 solution = GetSolution();
 string templatePath = "...";
 string genProjectPath = $"C:\\TestVSIX\\{projectName}\\";
 try {
     solution.AddFromTemplate(templatePath, genProjectPath, projectName);
 } catch (Exception ex) {
     System.Diagnostics.Debug.WriteLine(ex.Message);
 }


It seems to work properly to a point, the project is added, the files all seem to be included, but only some of the replacements seem to have happened.

...
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>
</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{9ED4A85F-9A60-4FEB-9736-7591511FE893}</ProjectGuid>
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>$safeprojectname$</RootNamespace>
<AssemblyName>$safeprojectname$</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<MvcBuildViews>false</MvcBuildViews>
<UseIISExpress>true</UseIISExpress>
<Use64BitIISExpress />
...


Where the matching proj file template chunk was defined as:

...
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>
</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{$guid1$}</ProjectGuid>
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>$safeprojectname$</RootNamespace>
<AssemblyName>$safeprojectname$</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<MvcBuildViews>false</MvcBuildViews>
<UseIISExpress>true</UseIISExpress>
<Use64BitIISExpress />
...


Note that $guid1$ updated while the $safeprojectname$ did not.
Additionally, $safeprojectname$ was not replaced in other files of the project as well.

What I have tried:

I attempted to bind a Wizard class to the template, but it doesn't seem to fire when adding the project using the AddFromTemplate method (research seemes to confirm this).

Additionally, I looked for some mechanism to force the replaces after calling AddFromTemplate, but I did not find any mention of such a thing.

I assume I could manually implement replacements, but before I bother, I'd like to understand why this isn't working properly.
Posted
Comments
Richard MacCutchan 23-Jan-19 4:23am    
I do not know the answer, but I have used this feature and never had a problem with it. Are you sure that your templates are being correctly updated before you try to use them? I have noticed that after adding a new template I have to restart Visual Studio to get the updated versions recognised.
johannesnestler 23-Jan-19 10:30am    
I just checked my templates and they look the same as yours and work… So Maybe have a look at your vstemplate… but as you showed it replaced at least the guid so <Project File="ProjectTemplate.csproj" ReplaceParameters="false"> can't be the Problem...
For ItemTemplates I also had to use a IWizard-Implementation - but I got it working as expected...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900