开发者

VSPackage: How to get selected file when there is no "real" solution opened

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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜