views:

496

answers:

3

I'm looking to create a Visual Studio 2008 template that will create a basic project and based on remove certain files/folders based on options the user enters.

Right now, I have followed some tutorials online which have let me create the form to query the user and pass the data into an IWizard class, but I don't know what to do from there.

The tutorials provide a sample to do some simple substitution: code:

Form1 form = new Form1();
DialogResult dlg = form.ShowDialog();
if (dlg == DialogResult.OK)
{
    foreach (KeyValuePair<string, string> pair in form.Parameters)
    {
        if (!replacementsDictionary.ContainsKey(pair.Key))
            replacementsDictionary.Add(pair.Key, pair.Value);
        else
            replacementsDictionary[pair.Key] = pair.Value;
    }
}
form.Close();

but I'm looking to selectively include files based on the user settings, and if possible, selectively include code sections in a file based on settings.

Is there a clever way to do this, or will I manually have to delete project files in the IWizard:ProjectFinishedGenerating()?

A: 

If I understand correctly, you want to be able to determine whether or not you should add project items to a project.

If so, you can implement IWizard.ShouldAddProjectItem and return whether or not you want the file to be added or not.

Erik
This is very helpful, but I would also like to know if its possible to selectively include parts of a file based on settings.The answer you provider here though is great. Thank you.
vanja.
+2  A: 

In my experience, ShouldAddProjectItem only gets called for folders in the template project. As such, it's pretty much useless.

Instead, you would need to put code in your ProjectFinishedGenerating implementation that uses the VS API to remove ProjectItems.

In there, you can remove items like this:

ProjectItem file = project.ProjectItems.Item("File.cs");
file.Remove();
mackenir
A: 

You can selectively include parts of a file by using $if$ with replacements. See, for example, this bit in the default C# Class Library template:

<ItemGroup>
    <Reference Include="System"/>
    $if$ ($targetframeworkversion$ >= 3.5)
    <Reference Include="System.Core"/>
    <Reference Include="System.Xml.Linq"/>
    <Reference Include="System.Data.DataSetExtensions"/>
    $endif$

...etc.

Roger Lipscombe