VS2010 扩展中文本视图的语言
我是为 Visual Studio 构建插件的新手,但已成功为 VS2010 构建了一个简单的工具,该工具在当前活动的代码窗口中执行一些文本操作。我已经到了需要了解当前文本视图的语言(VB.Net、C# 或其他语言)的地步。
我尝试使用以下代码获取文件名(以便我可以查看扩展名来确定语言):
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;
不幸的是,pathAsObject 始终返回 null。还有其他方法获取文件名/语言吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来这样有效:
这会返回 VB.Net 的“Basic”,这正是我需要知道的。
Looks like this works:
This returns "Basic" for VB.Net, which is exactly what I need to know.