locked
How to add a file to an in a project template existing folder? RRS feed

  • Question

  • Hi everybody,

    I've created a project template with several folders and files in.

    I'm using the iWizard Interface to add more files from network locations to the project, when the template is called.

    Something like that:

    public void ProjectFinishedGenerating(EnvDTE.Project project)

    {
    ....

    project.ProjectItems.AddFromFileCopy(@"I:\Projects\MyFile.dll");

    ....

    }


    I even managed to create folders and add files per Wizard like:

    EnvDTE.ProjectItem folderLib = project.ProjectItems.AddFolder("Libraries", EnvDTE.Constants.vsProjectItemKindPhysicalFolder);

    folderLib.ProjectItems.AddFromFileCopy(@"I:\Projects\MyFile.dll");



    But for technical reasons, some folder have to exist in the project template before I call the Wizard.

    So how to I add files in the ProjectFinishedGenerating method to folders already existing in my project template??


    Thanks in advance,

    mika

    • Edited by mika f Monday, June 16, 2008 1:36 PM Typing errors
    Monday, June 16, 2008 1:34 PM

Answers

  • You can get the ProjectItem interface representing that folder and then use its ProjectItem.ProjectItems Property as the collection representing all that project items that may be hierarchically below the project item, which is actually a ProjectItems interface. Then call AddFromFileCopy on this interface. And I write some macro for you:

    Dim proj As Project = DTE.Solution.Projects.Item(1) 
     
    'The project contains a folder namely TestFolder 
    proj.ProjectItems.Item("TestFolder").ProjectItems.AddFromFileCopy("f:\test.cs"
     

    This should be able to achieve this.

    Thanks!

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Proposed as answer by Feng Chen Wednesday, June 18, 2008 10:13 AM
    • Marked as answer by mika f Thursday, June 19, 2008 7:31 AM
    • Edited by Feng Chen Thursday, June 19, 2008 9:26 AM Typo corrected.
    Wednesday, June 18, 2008 10:13 AM

All replies

  • You can get the ProjectItem interface representing that folder and then use its ProjectItem.ProjectItems Property as the collection representing all that project items that may be hierarchically below the project item, which is actually a ProjectItems interface. Then call AddFromFileCopy on this interface. And I write some macro for you:

    Dim proj As Project = DTE.Solution.Projects.Item(1) 
     
    'The project contains a folder namely TestFolder 
    proj.ProjectItems.Item("TestFolder").ProjectItems.AddFromFileCopy("f:\test.cs"
     

    This should be able to achieve this.

    Thanks!

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Proposed as answer by Feng Chen Wednesday, June 18, 2008 10:13 AM
    • Marked as answer by mika f Thursday, June 19, 2008 7:31 AM
    • Edited by Feng Chen Thursday, June 19, 2008 9:26 AM Typo corrected.
    Wednesday, June 18, 2008 10:13 AM
  • Thank you very much Feng Chen!

    Everything works fine!

    mika
    Thursday, June 19, 2008 7:31 AM
  • You're too welcome! And if you have any other questions or concerns, please do not hesitate to contact us.  It is always our pleasure to be of assistance.

    Thanks!


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, June 19, 2008 9:25 AM