8

I am trying to create a VS package in which, I have added a menu command to the context menu, so it appears when you right click an item in the solution explorer. Now on clicking the command, I want to show a pop up with the details of the item, on which you right clicked and invoked the command.

Now how would I get information about the selected item? Is there any service I can use in order to get any details about the item?

Twenty
  • 5,234
  • 4
  • 32
  • 67
niruj
  • 125
  • 2
  • 9

1 Answers1

20
private static EnvDTE80.DTE2 GetDTE2()
    {
        return GetGlobalService(typeof(DTE)) as EnvDTE80.DTE2;
    }
private string GetSourceFilePath()
    {
        EnvDTE80.DTE2 _applicationObject = GetDTE2();
        UIHierarchy uih = _applicationObject.ToolWindows.SolutionExplorer;
        Array selectedItems = (Array)uih.SelectedItems;
        if (null != selectedItems)
        {
            foreach (UIHierarchyItem selItem in selectedItems)
            {
                ProjectItem prjItem = selItem.Object as ProjectItem;
                string filePath = prjItem.Properties.Item("FullPath").Value.ToString();
                //System.Windows.Forms.MessageBox.Show(selItem.Name + filePath);
                return filePath;
            }
        }
        return string.Empty;
    }

Above function will return the selected item(file) full path. basically get the soultion explorer from DTE2 instance and you will get all info about solution explorer from that.

gramcha
  • 692
  • 9
  • 16
  • Where to add these methods and from where we are going to invoke them. – Sharad Jul 13 '17 at 11:34
  • @Sharad it is like registering the callback for the event. You need link this function to the given menu item. – gramcha Jul 17 '17 at 11:51
  • This doesn't work for projects based on C++ template. Can you suggest an alternative? – Bandara Jun 14 '18 at 12:44
  • Sorry, @Bandara. I just noticed your question and I am no longer having windows and visual studio environment to try out your problem. – gramcha Dec 08 '19 at 13:51