locked
Adding a folder to a created project fails RRS feed

  • Question

  • I'm writing a feature extension with the feature builder power tools. After creating a new project based on the extension it's possible to add a new project via context menu. This context item will add a new silverlight project to the solution. This is working fine via project template. After adding the project I want to create a new folder within the added project but this fails because p.ProjectItems is null!

    p.ProjectItems.AddFolder("Test");
    

    Structure of the solution:

      Solution

        Solutionfolder ("Modules")

           Project 1

           Project 2

           Project 3 (<-- new added project via context menu)

               New Folder (<-- creating this folder within project 3 is not possible 

     

      var dte = ServiceProvider.GetService(typeof(DTE)) as EnvDTE.DTE;
    
      Solution4 solution = dte.Solution as Solution4;
      SolutionFolder solutionFolder = null;
      string newProjectpath = @"C:\test\";
    
      for (int solutionFolderIndex = 1; solutionFolderIndex < solution.Projects.Count - 1; solutionFolderIndex++)
      {
      // search for SolutionFolder "Modules"
      if (solution.Projects.Item(solutionFolderIndex).Name == "Modules")
      {
       solutionFolder = (SolutionFolder)solution.Projects.Item(solutionFolderIndex).Object;
    
       // Get Template Path for Silverlight project and add project to the solution folder
       String csTemplatePath = solution.GetProjectTemplate("SilverlightProject.zip", "CSharp");
       solutionFolder.AddFromTemplate(csTemplatePath, newProjectpath, "Project3");
    
       // Search for created project 
       for (int projectFolderIndex = 1; projectFolderIndex < solution.Projects.Item(solutionFolderIndex).ProjectItems.Count + 1; projectFolderIndex++)
       {
       if (solution.Projects.Item(solutionFolderIndex).ProjectItems.Item(projectFolderIndex).Name == "Project3")
       {
        ProjectItem p = solution.Projects.Item(solutionFolderIndex).ProjectItems.Item(projectFolderIndex);
        p.ProjectItems.AddFolder("New Folder");
       }
      }
      }
    
    


    Thursday, June 9, 2011 11:26 PM

Answers

  • Thanks Yi but I found the solution for this issue two days ago....

    It should be

    p.SubProject.ProjectItems.AddFolder("New Folder")

    • Marked as answer by Tobias Hatt Monday, June 13, 2011 12:05 PM
    Monday, June 13, 2011 12:04 PM

All replies

  • Hi Tobias,

     

    Based on your code, you are setting two loops to go through all the project items. The outer loop is for all projects and inner loop is for all project items for each project. Since the index of the projects and projectitems are from 1, for your outer loop,

    ·         for (int solutionFolderIndex = 1; solutionFolderIndex < solution.Projects.Count - 1; solutionFolderIndex++)

     

    it should be

    ·         for (int solutionFolderIndex = 1; solutionFolderIndex < solution.Projects.Count + 1; solutionFolderIndex++).

     

    To add a folder for the project3, Project3 is a project, we should add the folder from the Project level. In your code it is been added from Project Item level, which will fails, since there is no a project item called “Project3”. Therefore, the inner loop is not required for this case, the code should look like

     

    for (int solutionFolderIndex = 1; solutionFolderIndex < solution.Projects.Count+1; solutionFolderIndex++)

                        {

     

                          

                                Project pt = solution.Projects.Item(solutionFolderIndex) as Project;

                              

                                

                                if (pt.Name == "Project3")

                                {

                              

                                    pt.ProjectItems.AddFolder("New Folder");

                                }

                        }

     

    If I misunderstand you, please let me know.

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 13, 2011 7:57 AM
  • Thanks Yi but I found the solution for this issue two days ago....

    It should be

    p.SubProject.ProjectItems.AddFolder("New Folder")

    • Marked as answer by Tobias Hatt Monday, June 13, 2011 12:05 PM
    Monday, June 13, 2011 12:04 PM