Robin Holenweger
Robin Holenweger

Reputation: 343

How to programmatically reload an existing unloaded c# project with IVsSolution.CreateProject

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

Upvotes: 3

Views: 2675

Answers (2)

user2473950
user2473950

Reputation: 21

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", "");

Upvotes: 0

Carlos Quintero
Carlos Quintero

Reputation: 4414

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

Upvotes: 2

Related Questions