VSPackage:当没有“真实”文件时如何获取选定的文件解决方案已打开

发布于 2024-10-09 23:23:09 字数 646 浏览 0 评论 0原文

我正在用 C# 开发一个 VSPackage,我想知道当只有一个文件(或在 Visual Studio 中打开多个文件)时如何获取当前文件。

当存在层次结构时(打开一个真实解决方案),一切都可以正常工作。但是当我在 Visual Studio 中打开单个文件时,我无法获取当前选择。我的意思是打开 Visual Studio(没有解决方案)并选择文件 -> 打开 -> 文件

我需要获取其中之一:itemid、documentCookie、mkDocumentName 或其他。

我尝试了以下操作:

  • IVsMonitorSelection.GetCurrentSelection() ->它总是检索没有层次结构的解决方案项目 ID (Intpr.Zero)
  • IVsSelectionEvents ->我尝试监听选择事件,新的 itemId 始终是解决方案 itemid
  • RunningDocumentTable ->好吧,我很快就能找到解决方案,因为我可以枚举打开的文件,但我不确定如何确定哪个是当前文件。

I'm developing a VSPackage in C#, and I would like to know how to get the current file, when only a single file (or several files are opened in Visual Studio).

All works fine when there is a hierarchy (a real solution opened). But when I open a single file in Visual Studio, I cannot get the curren selection. I mean open Visual Studio (without solution) and select File -> Open -> File.

I need to get one of these: itemid, documentCookie, mkDocumentName or whatever.

I tried the following:

  • IVsMonitorSelection.GetCurrentSelection() -> It always retrieve the solution item id without hierarchy (Intpr.Zero)
  • IVsSelectionEvents -> I tried to listen the selection event, the new itemId is always the solution itemid
  • RunningDocumentTable -> Well, I'm close to find the solution, because I can enumerate the opened files, but I'm not sure how to determine which is the current one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁把谁当真 2024-10-16 23:23:09

这听起来像是 DTE 可以为您解决的问题。您是否检查过 DTE2.ActiveDocument(通过查询 SDTE 检索)?

var dte = (DTE2)GetService(typeof(SDTE));
var doc = dte.ActiveDocument;
// Check doc.Name, doc.Path, doc.FullName

This sounds like something that the DTE can solve for you. Have you checked DTE2.ActiveDocument (retrieved by querying for SDTE)?

var dte = (DTE2)GetService(typeof(SDTE));
var doc = dte.ActiveDocument;
// Check doc.Name, doc.Path, doc.FullName
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文