none
VSPackage: Force a project to unload then reload RRS feed

  • Question

  • Hello,

      In my flavored C# project when I add some of my custom project items I change the csproj file, which works fine, but some of the changes such as setting one file dependent on another doesn't show up unless I manually unload the project then reload it. Is there any anyway to do this in code once the project item has been added to the project?  

    Thanks,
    Kenny
    Tuesday, July 7, 2009 5:36 PM

Answers

  • Hi Kenny,

    My guess is you putting the code in a loop and the ExecuteCommand still failing, so it hit the break point forth.
    The conditions of "Project.UnloadProject available" are:
    1. Solution Explorer is actived when Auto-hide checked.
    2. Project is selected in Solution Explorer.

    We could use code ensure them:

    Window solExp = _applicationObject.ToolWindows.SolutionExplorer.Parent as Window; // Get the Solution Explorer Window
    solExp.Activate(); // Activte Solution Explorer Window
    UIHierarchyItem solHier = _applicationObject.ToolWindows.SolutionExplorer.UIHierarchyItems.Item(1); // Get solution hierarchy
    UIHierarchyItem projHier = solHier.UIHierarchyItems.Item(1); // Get the first project hierarchy
    projHier.Select(vsUISelectionType.vsUISelectionTypeSelect); // Select the first project
    _applicationObject.ExecuteCommand("Project.UnloadProject", ""); // Unload the first project
    System.Threading.Thread.Sleep(500);
    _applicationObject.ExecuteCommand("Project.ReloadProject", ""); // Reload the first project

    The code unload and reload the first project in your solution, you could modify it according your requirement.

    Sincerely,
    Wesley
    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Kenny Chinn Tuesday, July 14, 2009 1:58 PM
    Saturday, July 11, 2009 7:03 AM
  • Hi Kenny,

    I'm glad to hear it works.:)

    If we save the project before unloads it, the Save dialog will not appear anymore, so we just need:

    Project proj = _applicationObject.Solution.Projects.Item(1); // Get the first project
    proj.Save(proj.FullName); // Save it before unloading

    Please add these code before ExecuteCommand("Project.UnloadProject", "");

    Regards,
    Wesley
    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Kenny Chinn Tuesday, July 14, 2009 1:58 PM
    Tuesday, July 14, 2009 2:40 AM

All replies

  • Hi Kenny,

    We could get the DTE object in package and then call its method ExecuteCommand() to invoke the Unload and Reload commands.  i.e.:

    EnvDTE80.DTE2 dte2;
    dte2 = (EnvDTE80.DTE2)Package.GetGlobalService(typeof(SDTE));
    
    dte2.ExecuteCommand("Project.UnloadProject", "");
    System.Threading.Thread.Sleep(500);
    dte2.ExecuteCommand("Project.ReloadProject", "");

    Sincerely,
    Wesley
    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, July 9, 2009 6:58 AM
  • Wesley,
    Thanks for your repsonse. When I try and run the code you gave me I get a "HRESULT E_FAIL has been returned from a call to a COM component" error when  dte2.ExecuteCommand("Project.UnloadProject", ""); is called. Any idea on how to fix this? 

    Thanks,
    Kenny
    Thursday, July 9, 2009 2:30 PM
  • Hi Kenny,

    Thanks for your feedback.

    This error normally indicates the command "Project.UnloadProject" is unavailable in current context, where did you put it in?  Please make sure Project | Unload Project is visible in the menu when calling it.

    Regards,
    Wesley


    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, July 10, 2009 2:23 AM
  • Hi Wesley,
      Thanks for the response I moved the code to where the  command "Project.UnloadProject" is available, but now its getting stuck in recursion on these two lines:
    dte2 = (EnvDTE80.DTE2)Package.GetGlobalService(typeof(SDTE));
    dte2.ExecuteCommand("Project.UnloadProject", "");
    Once it hits that second line it just keeps going back and forth between that and the first line. Any ideas what's going on?

    Thanks,
    Kenny
    Friday, July 10, 2009 2:15 PM
  • Hi Kenny,

    My guess is you putting the code in a loop and the ExecuteCommand still failing, so it hit the break point forth.
    The conditions of "Project.UnloadProject available" are:
    1. Solution Explorer is actived when Auto-hide checked.
    2. Project is selected in Solution Explorer.

    We could use code ensure them:

    Window solExp = _applicationObject.ToolWindows.SolutionExplorer.Parent as Window; // Get the Solution Explorer Window
    solExp.Activate(); // Activte Solution Explorer Window
    UIHierarchyItem solHier = _applicationObject.ToolWindows.SolutionExplorer.UIHierarchyItems.Item(1); // Get solution hierarchy
    UIHierarchyItem projHier = solHier.UIHierarchyItems.Item(1); // Get the first project hierarchy
    projHier.Select(vsUISelectionType.vsUISelectionTypeSelect); // Select the first project
    _applicationObject.ExecuteCommand("Project.UnloadProject", ""); // Unload the first project
    System.Threading.Thread.Sleep(500);
    _applicationObject.ExecuteCommand("Project.ReloadProject", ""); // Reload the first project

    The code unload and reload the first project in your solution, you could modify it according your requirement.

    Sincerely,
    Wesley
    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Kenny Chinn Tuesday, July 14, 2009 1:58 PM
    Saturday, July 11, 2009 7:03 AM
  • Hi Wesley,
      Thanks for the response. I got it working now, but I have one issues I thought you might be able to help me with. When the UnloadProject gets called VS pops up a box asking if you want to save the project or not. Is there anyway to hide this message box and save the project in code so the user doesn't have to see it?  Thanks again for the help :).

    Thanks,
    Kenny
    • Edited by Kenny Chinn Monday, July 13, 2009 6:50 PM I can't spell
    Monday, July 13, 2009 6:16 PM
  • Hi Kenny,

    I'm glad to hear it works.:)

    If we save the project before unloads it, the Save dialog will not appear anymore, so we just need:

    Project proj = _applicationObject.Solution.Projects.Item(1); // Get the first project
    proj.Save(proj.FullName); // Save it before unloading

    Please add these code before ExecuteCommand("Project.UnloadProject", "");

    Regards,
    Wesley
    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Kenny Chinn Tuesday, July 14, 2009 1:58 PM
    Tuesday, July 14, 2009 2:40 AM
  • Hi Wesley,
    Thanks for all your help it's working great now, exactly what I wanted. :)

    Thanks for the help,
    Kenny
    Tuesday, July 14, 2009 6:47 PM
  • Hi

    I have same problem When I try and run the code you I get exception like a "HRESULT E_FAIL has been returned from a call to a COM component" error when  dte2.ExecuteCommand("Project.UnloadProject", ""); is called. In Project visible unload project in menu.
    Can You Please suggest any solution to resolve above issue?

    Thursday, February 16, 2017 4:58 AM
  • Hi 
    Thanks for all your help it's working now
    Thursday, February 16, 2017 6:40 AM