locked
How to add Folder using Item Template? RRS feed

  • Question

  •  

    Hi experts,

    I am going to create an Item Template which create 3 files in a folder.  I got it work with the a "hard-coded" folder name, however, how can I create the folder according to user input?

    Eg. I created the .vstemplate file as below:

        <Folder Name="Templates" TargetFolderName="$fileinputname$">
          <ProjectItem SubType="Code" TargetFileName="$fileinputname$A.cs" ReplaceParameters="true">file1.cs</ProjectItem>
          <ProjectItem SubType="Code" TargetFileName="$fileinputname$B.cs" ReplaceParameters="true">file2.cs</ProjectItem>
          <ProjectItem SubType="Code" TargetFileName="$fileinputname$C.cs" ReplaceParameters="true">file3.cs</ProjectItem>
        </Folder>

    When I give a name in the Add New Item dialog, the IDE create the folder with the name $fileinputname$ without replacing it with the parameter.

    So, is it possible to use Item Template in this way? If not, what is the easiest way to create a folder dynamically, according to user input?

    Thanks in advance!

    Monday, April 24, 2006 12:28 PM

Answers

  • Hello Tony,

    Unfortunately ItemTemplates does not support adding folders using replacement parameters as folder names. However you can create a CustomWizard that will help you to rename the folder once it has been added. You can read more about custom wizards at this link:

    http://msdn2.microsoft.com/en-us/library/ms185301.aspx

    Thanks,

    Ole

     Sample C# code on how to rename folder in ItemWizard:

    public class MyAddItemCustomWizard : Microsoft.VisualStudio.TemplateWizard.IWizard

    {

    private string folderName;

    private ProjectItem folderItem;

    #region IWizard Members

    public void BeforeOpeningFile(EnvDTE.ProjectItem projectItem)

    {

    }

    public void ProjectFinishedGenerating(EnvDTE.Project project)

    {

    }

    public void ProjectItemFinishedGenerating(EnvDTE.ProjectItem projectItem)

    {

    if (folderItem == null)

    folderItem = (ProjectItem)projectItem.Collection.Parent;

    if (string.IsNullOrEmpty(folderName))

    {

    //Determine foldername based on filename from current project Item by subtracting the last character of the name and the extension

    folderName = projectItem.Name.Remove(projectItem.Name.Length - 4);

    }

    }

    public void RunFinished()

    {

    // rename folder item

    if (folderItem != null && !string.IsNullOrEmpty(folderName))

    {

    folderItem.Properties.Item("FileName").Value = folderName;

    }

    }

    public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, Microsoft.VisualStudio.TemplateWizard.WizardRunKind runKind, object[] customParams)

    {

    }

    public bool ShouldAddProjectItem(string filePath)

    {

    return true;

    }

    #endregion

    }

    Monday, April 24, 2006 6:34 PM

All replies

  • Hello Tony,

    Unfortunately ItemTemplates does not support adding folders using replacement parameters as folder names. However you can create a CustomWizard that will help you to rename the folder once it has been added. You can read more about custom wizards at this link:

    http://msdn2.microsoft.com/en-us/library/ms185301.aspx

    Thanks,

    Ole

     Sample C# code on how to rename folder in ItemWizard:

    public class MyAddItemCustomWizard : Microsoft.VisualStudio.TemplateWizard.IWizard

    {

    private string folderName;

    private ProjectItem folderItem;

    #region IWizard Members

    public void BeforeOpeningFile(EnvDTE.ProjectItem projectItem)

    {

    }

    public void ProjectFinishedGenerating(EnvDTE.Project project)

    {

    }

    public void ProjectItemFinishedGenerating(EnvDTE.ProjectItem projectItem)

    {

    if (folderItem == null)

    folderItem = (ProjectItem)projectItem.Collection.Parent;

    if (string.IsNullOrEmpty(folderName))

    {

    //Determine foldername based on filename from current project Item by subtracting the last character of the name and the extension

    folderName = projectItem.Name.Remove(projectItem.Name.Length - 4);

    }

    }

    public void RunFinished()

    {

    // rename folder item

    if (folderItem != null && !string.IsNullOrEmpty(folderName))

    {

    folderItem.Properties.Item("FileName").Value = folderName;

    }

    }

    public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, Microsoft.VisualStudio.TemplateWizard.WizardRunKind runKind, object[] customParams)

    {

    }

    public bool ShouldAddProjectItem(string filePath)

    {

    return true;

    }

    #endregion

    }

    Monday, April 24, 2006 6:34 PM
  • Hi Ole Preisler ,

    I will give it a try. Thanks for your help!

    However, is there any simpler solution to create a folder with parameter replacement without write a line of code and GAC registration? Since, you know, to create a single folder, doing such many thing would be considered too much for me. But I truly appreciate your help!

     

    Tuesday, April 25, 2006 1:37 AM
  • I think there is a workaround that allows you to get the behaviour you want. In your template file specify $fileinputname$\ as part of the path in the TargetFileName attribute e.g.

    <ProjectItem
    ReplaceParameters="true"
    CustomTool="MSBuild:Compile" TargetFileName="$fileinputname$\$fileinputname$View.xaml"> PresentationModelUserControl1.xaml
    </ProjectItem>

    This will result in the folder being created and the relevant files which specify this path will be placed inside the folder.

    Note: if you do this, you may want to adjust the namespace definitions in all the files to append the folder name, assuming this is the behaviour you want.

    e.g. rather than 'namespace $rootnamespace$' use '$rootnamespace$.$fileinputname$'

    Other than that it seems to work ok.
    • Proposed as answer by Max Palmer Tuesday, July 29, 2008 10:56 AM
    Tuesday, July 29, 2008 10:56 AM