3

I'd like to add two buttons to my studio extension which unload/load all test projects of an open solution. After a lot of hassle, the unload works fine. Now I still have troubles loading them.

Somehow I cannot figure out how to correctly use IVsSolution.CreateProject() and I don't find any examples. Here is what I have so far, which always returns me VS_E_PROJECTALREADYEXISTS:

internal sealed class ProjInfo
{
    public ProjInfo(Guid guid, string name, string uniqueName)
    {
        Guid = guid;
        Name = name;
        UniqueName = uniqueName;
    }

    public Guid Guid { get; private set; }
    public string Name { get; private set; }
    public string UniqueName { get; private set; }

}

//....

private static void LoadProject(ProjInfo project, IVsSolution solutionService)
{
    IVsHierarchy selectedHierarchy;
    string solPath;
    string solFileName;
    string opstName;
    solutionService.GetSolutionInfo(out solPath, out solFileName, out opstName);
    string projRef;
    solutionService.GetProjectOfGuid(project.Guid, out selectedHierarchy);
    solutionService.GetProjrefOfProject(selectedHierarchy, out projRef);
    Guid projectType;
    IntPtr proj;
    solutionService.GetProjectTypeGuid(0, project.UniqueName, out projectType);
    projectType = Guid.Empty;
    var iidProject = Guid.Empty; //project.Guid;
    int res = VSConstants.S_OK;

    if (ErrorHandler.Failed(res = solutionService.CreateProject(ref projectType, solPath + project.UniqueName, null, null, (uint)__VSCREATEPROJFLAGS.CPF_OPENFILE, ref iidProject, out proj)))
    {
        Debug.Fail(String.Format("IVsolution::CreateProject retuend 0x{0:X}.", res));
    }
}

I played around with any combination of more creation-flags (__VSCREATEPROJFLAGS.CPF_OVERWRITE, __VSCREATEPROJFLAGS.CPF_SILENT), and without setting the references projectType and iidProject to Guid.Emtpy. Nothing works, I always get the result VS_E_PROJECTALREADYEXISTS.

Any idea how to achieve the functionality I have on the context menu to unload/reload a project programmatically? Maybe a completely different approach? Thanks a lot

Black Frog
  • 11,595
  • 1
  • 35
  • 66
Robin Holenweger
  • 321
  • 3
  • 14

2 Answers2

2

Using the automation model (EnvDTE), you can get the EnvDTE.DTE service from the package and assuming that the unloaded project node is selected in the Solution Explorer call dte.ExecuteCommand("Project.ReloadProject");

Alternatively, try IVsSolution4.ReloadProject

I have created two articles / samples:

HOWTO Unload/Reload a project from a Visual Studio package

Carlos Quintero
  • 4,300
  • 1
  • 11
  • 18
  • The problem I try to solve is for a solution with about 50+ test projects (and even more "normal" ones), with one button to unload all, and one to load them again. Having the projects selected in the solution explorer therefore doesn't sound right to me. I need something that can run in the "background". – Robin Holenweger Apr 20 '15 at 06:38
  • I have edited my answer. See it it meets your needs now. – Carlos Quintero Apr 20 '15 at 11:33
  • Wow, looks very promising! Thanks a lot already. Unfortunately I have time to look at it more closely only at the end of the current sprint (where we get some spare time to work on aiding tools like that). I'll come back to it. – Robin Holenweger Apr 21 '15 at 13:36
  • Sorry for the delay - project priorities change all the time. Finally was able to continue on the problem and it's solved thanks to your hint about the higher version API! Excellent work and examples. Thanks a lot! – Robin Holenweger Jul 27 '15 at 11:29
  • Link is now dead. – DCastenholz Mar 02 '23 at 00:49
0

We have unload and Reload the project using the project file also. Use the below code.

            Project.Save("projectfileFullPath");
            Project.DTE.ExecuteCommand("Project.UnloadProject", "");
            System.Threading.Thread.Sleep(500);
            Project.DTE.ExecuteCommand("Project.ReloadProject", "");