locked
Add Nested Project With SDK RRS feed

  • Question

  • Hello.
    I'm trying to create a solution programmatically, thow VSIX Command, adding existing projects. 
    Afert some tests i get it, but i want to move some projects to a solution folder.
    Is it posible?
    I`m try with IVsParentProject2.CreateNestedProject and IVsParentProject2.AddNestedSolution but fails.
    Thanks in advance.

    Here's my code:


       
    SVsSolution solutionService;
    IVsSolution solutionInterface;
    string solutionFullDirectory;
    solutionFullDirectory = "D:\\SVN\\trunk";
    solutionService = (SVsSolution)base.GetService(typeof(SVsSolution));   
    // Get the Solution interface of the Solution service   
    solutionInterface = solutionService as IVsSolution; 
    // Create the solution        solutionInterface.CreateSolution("D:\\Test", "TestSolution.sln", 0); 
    IntPtr myPointer = new IntPtr();
    int hr; 
    //here i call to different Interface without result.       
    hr = solutionInterface.CreateProject(VSConstants.CLSID.SolutionFolderProject_guid, null, null, "Libraries", (uint)__VSCREATEPROJFLAGS.CPF_NONLOCALSTORE, typeof(IVsParentProject2).GUID, out myPointer); 
    if (hr != VSConstants.S_OK) return false; 
    if (IntPtr.Zero != myPointer)        
    { 
        IVsParentProject2 parentProject = Marshal.GetObjectForIUnknown(myPointer) as IVsParentProject2; 
        Marshal.Release(myPointer); 
        if (parentProject != null)
        {                
            //hr=  parentProject.AddNestedSolution((uint)VSConstants.VSITEMID.Root, (uint)Microsoft.VisualStudio.Shell.Interop.__VSSLNOPENOPTIONS.SLNOPENOPT_DontConvertSLN, ps_path + "\\Libraries\\ExternalLibraries\\Project1.csproj");   
            hr = parentProject.CreateNestedProject((uint)VSConstants.VSITEMID.Selection, VSConstants.UICONTEXT.CSharpProject_guid, solutionFullDirectory + "\\Libraries\\ExternalLibraries\\Project1.csproj", null, null, (uint)__VSCREATEPROJFLAGS.CPF_OPENFILE, VSConstants.IID_IUnknown, new Guid(), out myPointer);      
        }
    } 
    hr = solutionInterface.CreateProject(VSConstants.CLSID.SolutionFolderProject_guid, null, null, "Test", (uint)__VSCREATEPROJFLAGS.CPF_NONLOCALSTORE, myGuid, out myPointer);
    if (hr != VSConstants.S_OK) return false;     
    //hr = solutionInterface.CreateProject(VSConstants.UICONTEXT.CSharpProject_guid, solutionFullDirectory + "\\Libraries\\ExternalLibraries\\ExternalLibraries\\Project1.csproj", null, null, (uint)__VSCREATEPROJFLAGS.CPF_OPENFILE, myGuid, out myPointer);      
    //if (hr != VSConstants.S_OK) return false;        hr = solutionInterface.CreateProject(VSConstants.UICONTEXT.VCProject_guid, solutionFullDirectory + "\\Libraries\\ExternalLibraries\\Project2.vcxproj", null, null, (uint)__VSCREATEPROJFLAGS.CPF_OPENFILE, myGuid, out myPointer);       
    if (hr != VSConstants.S_OK) return false;      
    hr = solutionInterface.CreateProject(VSConstants.UICONTEXT.CSharpProject_guid, solutionFullDirectory + "\\Libraries\\Project3\\Project3.csproj", null, null, (uint)__VSCREATEPROJFLAGS.CPF_OPENFILE, myGuid, out myPointer);  
    if (hr != VSConstants.S_OK) return false;
    // Save the solution       
    hr = solutionInterface.SaveSolutionElement((uint)__VSSLNSAVEOPTIONS.SLNSAVEOPT_ForceSave, null, 0);
    if (hr != VSConstants.S_OK) return false;

    Monday, September 4, 2017 8:33 AM

Answers

  • Here's my code:

                    DTE2 dte = this.GetService(typeof(SDTE)) as DTE2;
                    Solution2 sol = (Solution2)dte.Solution;
    
                    solutionFullDirectoryName = System.IO.Path.Combine(ps_path + "\\IntegrationServices", ps_name);
                    solutionFileName = ps_name + ".sln";
                    // Create the folder if it doesn't exist
                    if (!System.IO.Directory.Exists(solutionFullDirectoryName))
                    {
                        System.IO.Directory.CreateDirectory(solutionFullDirectoryName);
                    }
    
                    sol.Create(solutionFullDirectoryName, solutionFileName);
                    Project prjLibraries = sol.AddSolutionFolder("Libraries");
                    Project prjTests = sol.AddSolutionFolder("Test");
    
                    SolutionFolder SFLibraries = (SolutionFolder)prjLibraries.Object;
                    // Add a project to the new solution folder.
                    SFLibraries.AddFromFile(ps_path + "\\Libraries\\Project1\\Project1.vcxproj");
                    SFLibraries.AddFromFile(ps_path + "\\Libraries\\Project2\\Project2.csproj");
    
    
    
                    sol.SaveAs(solutionFileName);


    Wednesday, September 6, 2017 1:00 AM

All replies

  • Hi david alonso subiñas,

    >> but i want to move some projects to a solution folder.

    Do you mean you want to add an existing project into a solution? Or means just add the project in solution folder, not contained in .sln file?

    If you want to move an existing project into a solution (contained in .sln file), you could use _Solution.AddFromFile Method which could help us addd a project to the solution, based on a project file already stored in the system.

    If you just want add the project in solution folder without contained into .sln file, please try use SolutionFolder.AddSolutionFolder. Following document for AddSolutionFolder() contains the sample code. Hope that could help you.

    https://msdn.microsoft.com/en-us/library/envdte80.solutionfolder.addsolutionfolder.aspx

    Best Regards,
    Weiwei


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 5, 2017 6:28 AM
  • Finally I used DTE2 instead SVsSolution service.

    Thanks for advice.

    • Proposed as answer by Weiwei Cai Wednesday, September 6, 2017 12:51 AM
    Tuesday, September 5, 2017 11:00 AM
  • Hi david alonso subiñas,

    Thanks for sharing your solution here. If possible, please share some code sample here and mark it as answer, which is benefit to other communities who has the same problem.

    Best Regards,
    Weiwei


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 6, 2017 12:51 AM
  • Here's my code:

                    DTE2 dte = this.GetService(typeof(SDTE)) as DTE2;
                    Solution2 sol = (Solution2)dte.Solution;
    
                    solutionFullDirectoryName = System.IO.Path.Combine(ps_path + "\\IntegrationServices", ps_name);
                    solutionFileName = ps_name + ".sln";
                    // Create the folder if it doesn't exist
                    if (!System.IO.Directory.Exists(solutionFullDirectoryName))
                    {
                        System.IO.Directory.CreateDirectory(solutionFullDirectoryName);
                    }
    
                    sol.Create(solutionFullDirectoryName, solutionFileName);
                    Project prjLibraries = sol.AddSolutionFolder("Libraries");
                    Project prjTests = sol.AddSolutionFolder("Test");
    
                    SolutionFolder SFLibraries = (SolutionFolder)prjLibraries.Object;
                    // Add a project to the new solution folder.
                    SFLibraries.AddFromFile(ps_path + "\\Libraries\\Project1\\Project1.vcxproj");
                    SFLibraries.AddFromFile(ps_path + "\\Libraries\\Project2\\Project2.csproj");
    
    
    
                    sol.SaveAs(solutionFileName);


    Wednesday, September 6, 2017 1:00 AM