none
Delete a Project from solution

    Question

  • Hi,

    I want to delete a project from solution programatically. I use this code:

    EnvDTE80.DTE2 _applicationObject = (EnvDTE80.DTE2) this.GetService(typeof (SDTE));

                EnvDTE.UIHierarchy uih =
                    (EnvDTE.UIHierarchy) _applicationObject.Windows.Item
                                             (EnvDTE.Constants.vsWindowKindSolutionExplorer).Object;

                Project prj = _applicationObject.Solution.Projects.Item(1);
                prj.Delete();

     

    When I execute it, I get error message "Not implemented" when call prj.Delete();

    What I'm doing wrong?

    Thanks

    Tuesday, April 12, 2011 1:14 PM

Answers

  • While Project.Delete gives NotImplemented exception, you can remove the project from the solution by calling:

    _applicationObject.Solution.Remove(_applicationObject.Solution.Projects.Item(1))

    and the delete the files of the project from disk using the System.IO namespace.

     


    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/
    Tuesday, April 12, 2011 11:14 PM
    Moderator

All replies

  • Looking briefly at the DTE stuff for project (at least I think it is the DTE stuff from project, I am not on the project team) it does appear to simply return E_NOTIMPL :( 

    That said deleting a project can be a complex task (just looking at the delete handler in the solution explorer, there is a LOT of code, then again it handles multi-select and the like). It may be easiest if you were to select the node you want to delete in the solution explorer and then invoke the delete command programatically. 

    Alternatively you can select the node you want to delete, try casting it to IVsHierarchyDeleteHandler and calling DeleteItem on that, though going that route would possibly bypass any kind of UI prompting, which might surprise a user.

    Ryan

    Tuesday, April 12, 2011 4:56 PM
    Moderator
  • While Project.Delete gives NotImplemented exception, you can remove the project from the solution by calling:

    _applicationObject.Solution.Remove(_applicationObject.Solution.Projects.Item(1))

    and the delete the files of the project from disk using the System.IO namespace.

     


    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/
    Tuesday, April 12, 2011 11:14 PM
    Moderator