locked
Set new parent to a Project Item RRS feed

  • Question

  • Hello,

    I do not find a way to set a new Parent to a Project Item using EnvDTE...

    (Just to move a Project Item from one location (i.e. folder) to a new location (other folder))

    could someone help me on this?

    I also have access to IVsHierarchy (just in case)

    thank you so much in advance

    jesus

    Thursday, October 20, 2011 10:48 AM

Answers

  • Hi Jesus,

    If you focus on SDK way, as far as I know there is no a simple "Move" method, what we can do is to create a new project item and copy all the properties from the old one to the new one.

     

    DTE2 dte = _applicationObject;
    
                        ProjectItem pit1 = dte.Solution.FindProjectItem("aa.cs");
                        
                        ProjectItem pit2 = null;
    
                        foreach (ProjectItem pit in dte.Solution.Projects.Item(1).ProjectItems)
                        {
                            if (pit.Name == "aaa")
                                pit2 = pit;
    
                        }
                        
                        
                        ProjectItem pitn = pit2.ProjectItems.AddFromFileCopy(pit1.Document.FullName);
    
                        for (int i = 0; i < pitn.Properties.Count; i++)
                        {
                            try
                            {   // copy all properties
                                pitn.Properties.Item(i).Value = pit1.Properties.Item(i).Value;
                            }
                            catch (Exception e)
                            { 
                              // handle the excpetion for some properties which are not able to copy
                            }
                        }
                            pit1.Remove();
    

    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.

    • Marked as answer by VS Anywhere Monday, October 24, 2011 10:35 PM
    Monday, October 24, 2011 3:33 AM

All replies

  • Hello,

     

    If you want to move a file into a subfolder, you can find the folder and add the file into its projectitems. And then remove the original file. The following code demos to find a file called aa.cs and move it to a folder called aaa.

     

    DTE2 dte = _applicationObject;

     

                        ProjectItem pit1 = dte.Solution.FindProjectItem("aa.cs");

                        

                        ProjectItem pit2 = null;

     

                        foreach (ProjectItem pit in dte.Solution.Projects.Item(1).ProjectItems)

                        {

                            if (pit.Name == "aaa")

                                pit2 = pit;

     

                        }

                        

                        pit2.ProjectItems.AddFromFileCopy(pit1.Document.FullName);

                       

                        

                        

                        pit1.Remove();

     

    Another workaround is to cut and paste the file to the new folder using DTE automation. The following code is generate by Marco recorder, you may take a look and adapt it into your project.

     

            DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()

            DTE.ActiveWindow.Object.GetItem("ConsoleApplication16\ConsoleApplication16\aa.cs").Select(vsUISelectionType.vsUISelectionTypeSelect)

            DTE.ExecuteCommand("Edit.Cut")

            DTE.ActiveWindow.Object.GetItem("ConsoleApplication16\ConsoleApplication16\aaa").Select(vsUISelectionType.vsUISelectionTypeSelect)

            DTE.ExecuteCommand("Edit.Paste")

     

    Hope the workaround helps.

     

    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.

    • Marked as answer by VS Anywhere Friday, October 21, 2011 8:20 AM
    • Unmarked as answer by VS Anywhere Friday, October 21, 2011 10:34 AM
    Friday, October 21, 2011 5:51 AM
  • Thank you
    Friday, October 21, 2011 8:21 AM
  • Hi Yi,

     

    After doing some tests I found first workaround not suitable for my application, it works but it creates a complete new Project Item, I need to move a project item, not to create a new one, mainly because this have side effects on my addin.

    second option found something strange:

    1) you need to wait for Edit.Cut command to finish

    2) even if you wait for this to happen, if you raise Edit.Paste "too fast" (just after previous command finish) you can see how Solution Explorer shows the Project Item in the right location, and after half second is Deleted from Solution, to avoid this strange behaviour (?),  you need to "wait" for some undetermined amount of time before to raise Edit.Paste command.

    I understand both are workarounds, seems I can make second workaround  to work

    but there isn't really a formal way with SDK to do this? 

    thank you

    Friday, October 21, 2011 10:41 AM
  • Hi Jesus,

    If you focus on SDK way, as far as I know there is no a simple "Move" method, what we can do is to create a new project item and copy all the properties from the old one to the new one.

     

    DTE2 dte = _applicationObject;
    
                        ProjectItem pit1 = dte.Solution.FindProjectItem("aa.cs");
                        
                        ProjectItem pit2 = null;
    
                        foreach (ProjectItem pit in dte.Solution.Projects.Item(1).ProjectItems)
                        {
                            if (pit.Name == "aaa")
                                pit2 = pit;
    
                        }
                        
                        
                        ProjectItem pitn = pit2.ProjectItems.AddFromFileCopy(pit1.Document.FullName);
    
                        for (int i = 0; i < pitn.Properties.Count; i++)
                        {
                            try
                            {   // copy all properties
                                pitn.Properties.Item(i).Value = pit1.Properties.Item(i).Value;
                            }
                            catch (Exception e)
                            { 
                              // handle the excpetion for some properties which are not able to copy
                            }
                        }
                            pit1.Remove();
    

    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.

    • Marked as answer by VS Anywhere Monday, October 24, 2011 10:35 PM
    Monday, October 24, 2011 3:33 AM