简体   繁体   中英

How do I know what file was selected when the command was selected in the Solution Explorer

I'm writing an extension to Visual Studio using the Visual Studio Package project type. I'd like to add a command to the context menu when right clicking on files and folders in the solution explorer. I've added the command to the menu successfully but I can't seem to figure out how to retrieve what file or folder has been clicked on. How do i access that information?

You can get current selection from the Solution Explorer and cast it to ProjectItem:

EnvDTE.UIHierarchy solutionExplorer = dte.ToolWindows.SolutionExplorer;
object[] items = solutionExplorer.SelectedItems as object[];
EnvDTE.UIHierarchyItem item = items[0] as EnvDTE.UIHierarchyItem;
EnvDTE.ProjectItem projectItem = item.Object as EnvDTE.ProjectItem;
string path = projectItem.Properties.Item("FullPath").Value.ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM