none
Adding a new .cs file to a C# Project (not running in VS) programmtically ?

    Question

  • Hello,

    I want to add a new .cs file to already created Visual Studio 2010 C# project. This project lets say XYZ ,when I want to add a new file , it is not open in VS , so its not loaded in VS.

    Its on filesystem , lets say "c:\temp\XYZ.sln" , so I want to add my new A.cs file to this project programmtically , can I do this ?

    Regards,

    Sunny

    Thursday, October 07, 2010 7:34 AM

Answers

  • In your projectAdd a reference to "Microsoft.Build.Engine". Then try something like:

    Project proj = new Project();
    string projFileName = <yourProjFilePath>;
    proj.Load(projFileName);
    proj.AddNewItem("Compile", <yourItemFileName);
    proj.Save(projFileName);
    
    • Marked as answer by Sunnyz Saturday, October 09, 2010 1:14 PM
    Thursday, October 07, 2010 10:56 PM
  • Thanks Constantin ,

    But I couldn't find that how to add new 'DependentUpon' node , like incase of adding new WinForm. Can I do this using MS.Build namespace ?

    Sunny

    • Marked as answer by Sunnyz Saturday, October 09, 2010 1:14 PM
    Friday, October 08, 2010 7:10 AM
  • Please use:

    Project proj = new Project();
    string projFileName = <projFile>;
    proj.Load(projFileName);
    BuildItem item = proj.AddNewItem("Compile", <yourItemFileName>);
    item.SetMetadata("DependentUpon", <dependentUponItem>);
    proj.Save(projFileName);
    
    • Marked as answer by Sunnyz Saturday, October 09, 2010 1:14 PM
    Saturday, October 09, 2010 12:42 AM

All replies

  • Any help plz ?
    Thursday, October 07, 2010 12:20 PM
  • In your projectAdd a reference to "Microsoft.Build.Engine". Then try something like:

    Project proj = new Project();
    string projFileName = <yourProjFilePath>;
    proj.Load(projFileName);
    proj.AddNewItem("Compile", <yourItemFileName);
    proj.Save(projFileName);
    
    • Marked as answer by Sunnyz Saturday, October 09, 2010 1:14 PM
    Thursday, October 07, 2010 10:56 PM
  • Thanks Constantin ,

    But I couldn't find that how to add new 'DependentUpon' node , like incase of adding new WinForm. Can I do this using MS.Build namespace ?

    Sunny

    • Marked as answer by Sunnyz Saturday, October 09, 2010 1:14 PM
    Friday, October 08, 2010 7:10 AM
  • Please use:

    Project proj = new Project();
    string projFileName = <projFile>;
    proj.Load(projFileName);
    BuildItem item = proj.AddNewItem("Compile", <yourItemFileName>);
    item.SetMetadata("DependentUpon", <dependentUponItem>);
    proj.Save(projFileName);
    
    • Marked as answer by Sunnyz Saturday, October 09, 2010 1:14 PM
    Saturday, October 09, 2010 12:42 AM