How do I programmatically refresh/reload a VS project after modifying the underlying file?

前端 未结 2 1244
别跟我提以往
别跟我提以往 2021-01-05 04:47

I am developing a Visual Studio package and I have written some code that will make a file in Solution Explorer dependant upon another file.

What this means is that

相关标签:
2条回答
  • 2021-01-05 05:40

    AFAIK the only way of doing this is via automation of the Solution Explorer tool-window:

    EnvDTE.DTE dte = ...;
    
    string solutionName = Path.GetFileNameWithoutExtension(dte.Solution.FullName);
    string projectName = project.Name;
    
    dte.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer).Activate();
    ((DTE2)dte).ToolWindows.SolutionExplorer.GetItem(solutionName + @"\" + projectName).Select(vsUISelectionType.vsUISelectionTypeSelect);
    
    dte.ExecuteCommand("Project.UnloadProject");
    dte.ExecuteCommand("Project.ReloadProject");
    

    Note that, if the project hasn't been saved, the user will get a dialog box prior to the "Project.UnloadProject" call.

    0 讨论(0)
  • 2021-01-05 05:50

    Here is my code (with reactivating the old window):

    public void RefreshSolutionExplorer(EnvDTE.Project activeProject, string captionOfActiveWindow)
    {
      DTE2 dte2 = activeProject.DTE as DTE2;
      string solutionName = Path.GetFileNameWithoutExtension(dte2.Solution.FullName);
      string projectName = activeProject.Name;
    
      // Activate SolutionExplorer window
      dte2.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate();
      // Select your project to be updated
      dte2.ToolWindows.SolutionExplorer.GetItem(solutionName + @"\" + projectName).Select(vsUISelectionType.vsUISelectionTypeSelect);
      // Refresh SolutionExplorer window
      dte2.ExecuteCommand("View.Refresh", String.Empty);
      // Reactivate your old window
      dte2.Windows.Item(captionOfActiveWindow).Activate();
    }
    
    0 讨论(0)
提交回复
热议问题