locked
Adding a project to Folder in a Visual Studio Template RRS feed

  • Question

  • I have created a multi project Visual Studio Template following these instructions:

    http://msdn.microsoft.com/en-us/library/ms185308.aspx.

    This works fine and I can see my two projects in Visual Studio when I select my project template from the File-->New Project option.

    I have followed this link http://msdn.microsoft.com/en-us/library/ms185301.aspx and I am able to add new folders to my solution by doing the following in the CustomWizard.

     public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
     {     
         _dte = automationObject as _DTE;
     }
    
     public void RunFinished()
     {
         _solution = (Solution2)_dte.Solution;
     
         _solution.AddSolutionFolder("MyFolder1");
         _solution.AddSolutionFolder("MyFolder2");    
     }

    But I want to add a certain project that is in my template under MyFolder1 and add another project that is in my template under MyFolder2.

    At present with the above code my projects will be loaded from the template AND there will be 2 folders created beside them.  I want the projects under the folders.

    How can I do this??

    Thanks for any help you can give :)
    • Edited by LearnerMan1234 Tuesday, April 29, 2014 4:46 PM
    • Moved by Caillen Thursday, May 1, 2014 3:16 AM
    Tuesday, April 29, 2014 4:18 PM

Answers

  • Hi,

    Sorry for delay.

    Thank you for your feedback!

    According to your further information, I see you want to locate a project in a specific folder.

    Please refer to the similar thread. 

    ENVDTE - Add new project to existing solution and locate it in a specific folder

    Here is a code snippet.

    Project project = getSolutionSubFolder(solution, "SubFolderName");
    if (project != null)
    {
        SolutionFolder folder = (SolutionFolder)project.Object;
        folder.AddFromFile("yourProjectFilePath");
    }Where getSolutionSubFolder method looks like this: 
    
    private static Project getSolutionSubFolder(Solution2 solution, string subfolder)
    {
        return 
            solution
                .Projects
                .Cast<Project>()
                .FirstOrDefault(
                p => string.Equals(p.Name, subfolder, StringComparison.Ordinal));
    }
    Best regards,


    • Edited by Anna Cc Thursday, May 29, 2014 3:25 PM
    • Marked as answer by Anna Cc Wednesday, June 4, 2014 1:45 PM
    Thursday, May 29, 2014 3:25 PM

All replies

  • Hi Learnerman1234,

    Actually this is a problem with Visual Studio Extensibility,  this forum is the right place for your question:

    Wednesday, April 30, 2014 3:02 PM
  • Hi,

    Solution2.AddSolutionFolder Method :  Adds a solution folder to a ProjectItems collection.

    I think you have added the two folders to the solution.

    Do you mean you want to create a template with 2 projects, and each of the two projects under a folder? 

    If so, you can add project to a folder manually. Please refer to,

    Create a new folder in a project

    http://msdn.microsoft.com/en-us/library/cc294731.aspx

    Best regards,

    Sunny


    • Edited by Anna Cc Thursday, May 1, 2014 9:54 AM
    Thursday, May 1, 2014 9:52 AM
  • Sunny,

    I already have a template with two projects.  When I create a new project I select the template and it loads the two projects into my new solution.

    But I want to have project1 under folder1 and project2 under folder2. I want this to be done automatically and not a manual step.

    Friday, May 2, 2014 8:54 AM
  • Hi,

    Sorry for delay.

    Thank you for your feedback!

    According to your further information, I see you want to locate a project in a specific folder.

    Please refer to the similar thread. 

    ENVDTE - Add new project to existing solution and locate it in a specific folder

    Here is a code snippet.

    Project project = getSolutionSubFolder(solution, "SubFolderName");
    if (project != null)
    {
        SolutionFolder folder = (SolutionFolder)project.Object;
        folder.AddFromFile("yourProjectFilePath");
    }Where getSolutionSubFolder method looks like this: 
    
    private static Project getSolutionSubFolder(Solution2 solution, string subfolder)
    {
        return 
            solution
                .Projects
                .Cast<Project>()
                .FirstOrDefault(
                p => string.Equals(p.Name, subfolder, StringComparison.Ordinal));
    }
    Best regards,


    • Edited by Anna Cc Thursday, May 29, 2014 3:25 PM
    • Marked as answer by Anna Cc Wednesday, June 4, 2014 1:45 PM
    Thursday, May 29, 2014 3:25 PM