Reputation: 31847
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 itemidRunningDocumentTable
-> 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.Upvotes: 2
Views: 1187
Reputation: 19781
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
Upvotes: 5