locked
EnvDTE.ProjectItem.Delete() always throws when debugging RRS feed

  • Question

  • Am I doing something wrong, or is there a way around this problem ?

    using System.Runtime.InteropServices;
    using EnvDTE;
    using EnvDTE100;
    using EnvDTE80;
    
    namespace Codyflex.VisualStudioAutomation
    {
      public class VsTest
      {
        public static void Test()
        {
          OleMessageFilter.Register(); // See: http://msdn.microsoft.com/en-us/library/ms228772.aspx
    
          try {
            DTE2    dte2 = (DTE2) Marshal.GetActiveObject("VisualStudio.DTE.10.0");
            Solution4  solution = (Solution4) dte2.Solution;
            Project   project = solution.Item(@"TestTarget\TestTarget.csproj");
            ProjectItem item = project.ProjectItems.Item("Class1.cs");
    
            item.Delete();
    
            // When run in debug mode, item.Delete() always throws
            // COMException "The item 'Class1.cs' cannot be deleted."
            //
            // Works OK if run without debugging.
          }
          finally {
            OleMessageFilter.Revoke();
          }
        }
      }
    }
    
    

    Regards

    FrankG

     

     

    Wednesday, May 4, 2011 9:33 AM

Answers

  • Hi Frank,

     

    Based on your code, it seems that you want to automate the Visual Studio to delete the files in current project/solution. If so, the COM exception during debug mode is expected.

     

    When we use visual studio debugger to debugging the solution, the debugger will lock all the files in the solution into read only status. Therefore the file cannot be deleted by the application itself. However, if we run the application without debugging, the class1.cs is modifiable, we can delete it without any problem.

     

    As a workaround, we can remove the class1.cs file from the project first, then call System.IO.File.Delete to delete the file from the harddisk.    

     

                try

                {

                    DTE2 dte2 = Marshal.GetActiveObject("VisualStudio.DTE.10.0") as DTE2;

                    Solution4 solution = dte2.Solution as Solution4;

                  

                    Project project = solution.Item(@"TestAutomation.csproj");

                  

                    ProjectItem item = project.ProjectItems.Item("Class1.cs");

                    String filename = item.get_FileNames(0);

                    item.Remove(); // remove the item from project

                    System.IO.File.Delete(filename); //delete the file

     

                }

                finally

                {

                    MessageFilter.Revoke();

                }

     

     

    Hope the information helps. If I misunderstand you,please let me know.

     

    Regards,

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 6, 2011 9:34 AM

All replies

  • Hi Frank,

     

    Based on your code, it seems that you want to automate the Visual Studio to delete the files in current project/solution. If so, the COM exception during debug mode is expected.

     

    When we use visual studio debugger to debugging the solution, the debugger will lock all the files in the solution into read only status. Therefore the file cannot be deleted by the application itself. However, if we run the application without debugging, the class1.cs is modifiable, we can delete it without any problem.

     

    As a workaround, we can remove the class1.cs file from the project first, then call System.IO.File.Delete to delete the file from the harddisk.    

     

                try

                {

                    DTE2 dte2 = Marshal.GetActiveObject("VisualStudio.DTE.10.0") as DTE2;

                    Solution4 solution = dte2.Solution as Solution4;

                  

                    Project project = solution.Item(@"TestAutomation.csproj");

                  

                    ProjectItem item = project.ProjectItems.Item("Class1.cs");

                    String filename = item.get_FileNames(0);

                    item.Remove(); // remove the item from project

                    System.IO.File.Delete(filename); //delete the file

     

                }

                finally

                {

                    MessageFilter.Revoke();

                }

     

     

    Hope the information helps. If I misunderstand you,please let me know.

     

    Regards,

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 6, 2011 9:34 AM
  • Many thanks Yi.  We found the same solution.

    Regards

    FrankG

    Monday, May 9, 2011 8:29 AM
  • Thanks Yi.

    For anyone having this problem using Entity framework and EF6.Utility.CS.ttinclude

    Find this row (~1846):

    pair.Value.Delete();

    And change to the suggested:

    String filename = pair.Value.ToString(); pair.Value.Remove(); System.IO.File.Delete (filename);

    Cheers!

    Tuesday, February 4, 2014 12:37 PM