Can I make an item template that will insert multiple files into different projects under the same solution?
I am NOT looking to add files under different folders in the same project.
This is what I am looking for:
Solution
Project 1
Project 2
Ok based on VS 2017 I created one VSIX project one Library Project and one Item Template Project
1- In Item template project I create both item template files one and two. I also added wizardExtension info in vstemplate file like this
<TemplateContent>
<ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$Service.cs">Service.cs</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$DTO.cs">DTO.cs</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$Message.cs">Message.cs</ProjectItem>
</TemplateContent>
<WizardExtension>
<Assembly>WizardImplementation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</Assembly>
<FullClassName>WizardImplementation.WizardImplementation</FullClassName>
</WizardExtension>
WizardImplementation is my class library project.
2- I added TemplateWizardInterface Nuget Package to the Class Library Project and implement IWizard in a class. In ProjectItemFinishedGenerating I am moving the second template item like a file to the second project location folder and add it to project programmatically using Microsoft.Build.Evaluation.ProjectCollection in Microsoft.Build dll.
public void ProjectItemFinishedGenerating(ProjectItem projectItem)
{
if (projectItem.FileNames[0].Contains("DTO") || projectItem.FileNames[0].Contains("Message"))
{
string newPath = Path.GetFullPath(Path.Combine(projectItem.FileNames[0], @"..\Project2\"));
if (!Directory.Exists(newPath))
{
MessageBox.Show($"Message path does not exist. \r\n {newPath}");
}
else
{
var newFullPath = Path.Combine(newPath, projectItem.Name);
File.Move(projectItem.FileNames[0], newFullPath);
var p = Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection.GetLoadedProjects(PathInformation.MessageProjectPath).FirstOrDefault();
if (p == null)
p = new Microsoft.Build.Evaluation.Project(PathInformation.MessageProjectPath);
var res = p.AddItem("Compile", newFullPath);
p.Save();
if(res == null)
{
MessageBox.Show("Nothing added to project");
}
else
{
MessageBox.Show($"{res.Count()} item added to project");
}
}
}
}
for more information about IWizard look at this link https://msdn.microsoft.com/en-us/library/ms185301.aspx
3- In VSIX project I added the Class Library I created in second step to the Assets by type of Assembly into vsixmanifest. Also I added the item template project as a File and give the zip file created by VS after build of Item Project in bin.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With