none
How to create new Csharp Solution, Project programmatically ( EnvDTE ) ??

    Question

  •  

    Hi,

     

              I am looking for the code that will create a new Solution and Add a C# project in the Solution. I know we can use the below code with the Add-ins project ..

    But just want to know is this possible from the Console Application because I have tried a lot ..but getting an exception to create a new Solution ..

     

     

    using EnvDTE;

    using EnvDTE80;

     

     

     

    Solution soln = new Solution();// templates.

    soln.Create("path","name");

     

    or

     

    Solution2 soln = (Solution2)dte.Solution;

     

     

    csTemplatePath = soln.GetProjectTemplate("MyMainTemplate.zip", "CSharp");

    soln.AddFromTemplate(csTemplatePath, csPrjPath, txtProjectName.Text.Trim(), false);

     

     

    Help will be appreciated ...

     

     

     

    Tuesday, December 11, 2007 11:06 AM

Answers

  • Yes I figured it out we have to use the below code instead.. We have to take the DTE object from the reflection and take the Solution property of it.

     

    But thanks to Ole for giving a right clue and also once again Reflection showed it's importance Smile

     

     

     

    System.Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE.8.0");

    Object obj = System.Activator.CreateInstance(type, true);

    EnvDTE80.DTE2 dte8Obj = (EnvDTE80.DTE2)obj;

    Solution2 soln = (Solution2)dte8Obj.Solution;

    soln.Create("<PATH>", "<SOLUTION NAME>");

    soln.DTE.MainWindow.Visible = true;

    string csTemplatePath = soln2.GetProjectTemplate("ConsoleApplication.zip", "CSharp");

    soln.AddFromTemplate(csTemplatePath, @"C:\temp\FooBar", "Foo", false);

    int x = soln.AddIns.Count;

     

    Wednesday, December 12, 2007 11:52 AM

All replies

  • Add a reference to EnvDTE and EnvDTE80 and copy and paste this code code snippet into your consoleApp project. That will do the correct instantiation of VS and create a solution from template.

     

    Code Block

    EnvDTE.Solution soln = System.Activator.CreateInstance(Type.GetTypeFromProgID("VisualStudio.Solution")) as EnvDTE.Solution;

    soln.DTE.MainWindow.Visible = true;

    EnvDTE80.Solution2 soln2 = soln as EnvDTE80.Solution2;

    string csTemplatePath = soln2.GetProjectTemplate("ConsoleApplication.zip", "CSharp");

    soln.AddFromTemplate(csTemplatePath, @"C:\temp\FooBar", "Foo", false);

    int x = soln.AddIns.Count;

     

     

     

    Ole

    • Proposed as answer by ShailiShah Tuesday, January 24, 2012 11:50 AM
    • Unproposed as answer by ShailiShah Tuesday, January 24, 2012 11:50 AM
    Tuesday, December 11, 2007 11:23 PM
  • Hi Ole..

     

         Thanks for your great help ..as the above code moved me one step up .. but below code throws and exception as soln2 gets the null reference ..  can you help me ahead of this ..

     

    ( just asking can't we create the solution2 class object from reflection or any other way to find the ProjectTemplate path ..)

     

     

     

    EnvDTE80.Solution2 soln2 = soln as EnvDTE80.Solution2;

    string csTemplatePath = soln2.GetProjectTemplate("ConsoleApplication.zip", "CSharp");

     

    Wednesday, December 12, 2007 10:57 AM
  • Yes I figured it out we have to use the below code instead.. We have to take the DTE object from the reflection and take the Solution property of it.

     

    But thanks to Ole for giving a right clue and also once again Reflection showed it's importance Smile

     

     

     

    System.Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE.8.0");

    Object obj = System.Activator.CreateInstance(type, true);

    EnvDTE80.DTE2 dte8Obj = (EnvDTE80.DTE2)obj;

    Solution2 soln = (Solution2)dte8Obj.Solution;

    soln.Create("<PATH>", "<SOLUTION NAME>");

    soln.DTE.MainWindow.Visible = true;

    string csTemplatePath = soln2.GetProjectTemplate("ConsoleApplication.zip", "CSharp");

    soln.AddFromTemplate(csTemplatePath, @"C:\temp\FooBar", "Foo", false);

    int x = soln.AddIns.Count;

     

    Wednesday, December 12, 2007 11:52 AM
  • hi hero ,

    thank you very very much..

    you helped me a lot

    i dont have words to say ........U are Great .....

    so nice of you .....

     


    Thanks & Regards, K.BhaskarReddy
    Monday, February 02, 2009 2:26 PM
  • Hi all,

     

         And, I need to create a C++  ->   SmartDevice  ->  ATLSmartDeviceProject

     

     

    Could anyone please tell me?

    Friday, May 27, 2011 2:05 PM
  • hi ,

         thank you very much for this code.

        Now i want to add some reference programatically with other references in new solution like -using NLog;

       what should i do ??


    • Edited by ShailiShah Tuesday, February 07, 2012 12:13 PM
    Tuesday, February 07, 2012 12:06 PM
  • I know this is a old thread, but thought may be some one would be assisted by this.

    Adding a reference is easily done.

     Here is the code (Not tested) - This assumes the reference is not there and just tries to insert it, how ever, a good idea will be to run down a try catch block in order to first check that the reference exists and then add it. This will just add it to the Active Project

    // Needs to be there for a VSProject Class using VSLangProj; // I am going to add 2 Framework References // If you want to add a custom reference, just put the full path here instead of just the name void addReference()

    {

    // For a Visual Studio Project VSProject obj = (VSProject)GetActiveProject.Object; string[] RefList = new string[2] { "System.Data.Services.Client", "System.Drawing" }; for (int i = 0; i < (int)RefList.Length; i++) { string refName = RefList[i]; if (!string.IsNullOrEmpty(refName)) { obj.References.Add(refName); } }

    }

    The GetActiveProject function will be something like this 

    Project GetActiveProject()
    {
         // I am just declaring this as indicative, you will need to pass this
         DTE2 appObj; 
         Array activeSolutionProjects = this.appObj.DTE.ActiveSolutionProjects as Array;
                        
         if (activeSolutionProjects != null && activeSolutionProjects.Length > 0)
         {
             Project myProj = activeSolutionProjects.GetValue(0) as Project;
             return myProj;
         }
         else 
         { 
              return null;
         }
    }


    Tuesday, November 26, 2013 8:31 PM