2

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.
Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219

1 Answers1

5

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
sisve
  • 19,501
  • 3
  • 53
  • 95