gpt4 book ai didi

visual-studio-extensions - Visual Studio 扩展 : Get the path of the current selected file in the Solution Explorer

转载 作者:行者123 更新时间:2023-12-02 01:30:17 33 4
gpt4 key购买 nike

我正在尝试为 Visual Studio 创建我的第一个扩展,到目前为止,我一直在关注本教程以帮助我入门( http://www.diaryofaninja.com/blog/2014/02/18/who-said-building-visual-studio-extensions-was-hard )。
现在,当我单击解决方案资源管理器中的文件时,会出现一个自定义菜单项。
我现在需要为我的小项目获取在解决方案资源管理器中选择的文件的路径,但我不明白我该怎么做。
有什么帮助吗?

- - - - - - - - - - - - - - 编辑 - - - - - - - - - - - ---------

正如 matze 所说,答案在我发布的链接中。只是写的时候没注意。
与此同时,我还在这个帖子中找到了另一个可能的答案:How to get the details of the selected item in solution explorer using vs package

我在哪里找到了这个代码:

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;
}

因此,这里有两种方法可以获取所选文件的路径:)

最佳答案

您提到的文章已经包含了解决方案。

在示例代码中查找 menuCommand_BeforeQueryStatus 方法。它使用 IsSingleProjectItemSelection 方法获取表示项目的 IVsHierarchy 对象以及所选项目的 id。似乎您可以安全地将层次结构转换为 IVsProject 并使用它的 GetMkDocument 函数来查询项目的完整路径...

IVsHierarchy hierarchy = null;
uint itemid = VSConstants.VSITEMID_NIL;

if (IsSingleProjectItemSelection(out hierarchy, out itemid))
{
IVsProject project;
if ((project = hierarchy as IVsProject) != null)
{
string itemFullPath = null;
project.GetMkDocument(itemid, out itemFullPath);
}
}

我不想将文章中的整个代码复制到这个答案中,但可能对 IsSingleProjectItemSelection 函数如何获取所选项目感兴趣;所以我只是添加一些注释,它们可能会引导到正确的方向......该方法使用全局 GetCurrentSelection 服务的 IVsMonitorSelection 方法来查询当前选定的项目。

关于visual-studio-extensions - Visual Studio 扩展 : Get the path of the current selected file in the Solution Explorer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34677290/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com