I'm new to building addins for Visual Studio, but have managed to build a simpe tool for VS2010 that does a little text manipulation in the currently active code window. I've got to the point where I need to know the language (VB.Net, C# or whatever) of the current text view.
I have tried to get the filename (so I can look at the extension to determine the language) using the the following code:
IVsTextManager txtMgr = (IVsTextManager)GetService(typeof(SVsTextManager));
int mustHaveFocus = 1;//means true
txtMgr.GetActiveView(mustHaveFocus, null, out currentTextView);
userData = currentTextView as IVsUserData;
if (userData == null)// no text view
{
Console.WriteLine("No text view is currently open");
return;
}
object pathAsObject;
Guid monikerGuid = typeof(IVsUserData).GUID;
userData.GetData(ref monikerGuid, out pathAsObject);
string docPath = (string)pathAsObject;
Unfortunately pathAsObject always returns null. Is there any other way to get the filename / language?