C#:选择当前活动的文件 [英] C#: Selecting currently active file
问题描述
所以我正在尝试为Visual Studio 2012创建一个加载项,我被卡住了。所以这就是我被困住的地方。
假设在Visual Studio中我正在处理一个文件。假设它叫做Default.aspx。我的加载项的工作原理如下:当你按下工具栏上的按钮时,它会将文件中的所有内容复制到一个字符串变量并用它做一些事情。
那么我可以使用哪个函数来查找当前打开文件的路径?我可以在visual studio中打开4-5个选项卡,但我只想找到当前正在处理的文件的路径,在此上下文中将是Default.aspx。有没有办法做到这一点?
你想使用应用程序参数,强制转换为_DTE类:
applicationObject =(_ DTE)application;
然后你可以使用 ActiveDocument [ ^ ]物业。
不能我真的明白你的答案可以详细解释吗?
当你写一个插件时,VS会调用你的方法,并提供一些参数:一个这些是应用程序
作为对象。
(见这个,它可能会有所帮助:如何创建从项目窗口启动的Visual Studio加载项 [ ^ ])
您需要将该对象转换为_DTE,然后您可以访问ActiveDocument属性 - 应该允许您访问Default.aspx文件。 / blockquote>
如果您正在使用桌面应用程序,您可以尝试
var lstFormCollection = Application.OpenForms; - 将返回所有活动页面
在网上
字符串currentPageFileName = new FileInfo(this.Request.Url.LocalPath).Name;
----仅返回当前页面
So I'm trying to create an add-in for Visual Studio 2012 and I'm stuck. So here's where I'm stuck.
Let's say in Visual Studio I'm working on a file. Assume it's called Default.aspx . My add-in works like this: When you press the button on the toolbar, it will copy everything on the file to a string variable and do some stuff with it.
So which function can I use to find the path of the current open file? I could have 4-5 tabs open in visual studio but I only want to find the path of the file which is currently being worked on which would be Default.aspx in this context. Is there a way to do this?解决方案You want to use the application parameter, cast as an _DTE class:
applicationObject = (_DTE)application;
You can then use the ActiveDocument[^] Property.
"Couldn't really understand your answer could you elaborate?"
When you write an addin, VS calls your method, and provides a number of parameters: one of these is theapplication
as an object.
(see this, it might help: How to Create a Visual Studio Add-in that Launches from the Project Window[^])
You need to cast that object to a _DTE, and you can then access the ActiveDocument property - should give you access to your Default.aspx file.
If you are using Desktop application you can try
var lstFormCollection = Application.OpenForms;--Will return all active page
In web
string currentPageFileName = new FileInfo(this.Request.Url.LocalPath).Name;
----Will return current page only
这篇关于C#:选择当前活动的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!