読者です 読者をやめる 読者になる 読者になる

HOWTO: Reopen the currently loaded solution from a VS Extension

Motivation

Suppose you are developing user property sheets in your Visual C++ project. Dealing with the Property Manager is really troublesome, so you start to develop a VS extension. The VS entension manages which property sheets to import and edits property sheets.

You encounter a problem. If you edit a property sheet outside the IDE, the IDE will not automatically reload the property sheet and therefore such changes will not be reflected in the IDE. There seems to be no API for reloading property sheets. Removing the property sheet and re-adding it does not work either; property sheets seem to be cached somewhere.

In order to have the IDE reload property sheets, the only workaround I have found is reopening the entire solution. Let's do it.

Environment

Code Fragment

private void MenuItemCallback(object sender, EventArgs e)
{
    // Obtain the SVsSolution service object.
    var solutionService =
        this.ServiceProvider.GetService(typeof(SVsSolution)) as IVsSolution;

    // Check if there exists an open solution.
    object isSolutionOpen;
    solutionService.GetProperty(
        (int)__VSPROPID.VSPROPID_IsSolutionOpen,
        out isSolutionOpen);
    if (!(bool)isSolutionOpen)
    {
        return;
    }

    // Retrieve the information on the open solution.
    // Full path to the directory cotaining the .sln file
    string solutionDirectory;   
    // Full path to the .sln file
    string solutionFile;
    // Full path to the .suo file
    string userOptsFile;
    solutionService.GetSolutionInfo(
        out solutionDirectory,
        out solutionFile,
        out userOptsFile);

    // Close the entire solution. (pHier == null, docCookie == 0)
    solutionService.CloseSolutionElement(
        // Prompt the user whether to save unsaved changes.
        (uint)__VSSLNSAVEOPTIONS.SLNSAVEOPT_PromptSave, 
        null,
        0);

    // Reopen the solution.
    solutionService.OpenSolutionFile(0, solutionFile);
}

References