[英]Get file path from Visual Studio editor
I'm developing a Visual Studio Package, written in C#. 我正在开发一个用C#编写的Visual Studio包。
How do I get the full path of the active editor programatically? 如何以编程方式获取活动编辑器的完整路径?
This is how you get the full path of the focused (active) document in Visual Studio: 这是您在Visual Studio中获取焦点(活动)文档的完整路径的方法:
DTE dte = (DTE)GetService(typeof(DTE));
string document = dte.ActiveDocument.FullName;
When working macros you can use 在使用宏时,您可以使用
DTE.ActiveDocument.Path + DTE.ActiveDocument.Name
to get the full path. 获得完整的路径。 Likely this is the same in C# when making VS packages?
在制作VS包时,C#中的可能性是一样的吗?
I had a similar problem when developing ASP.NET Web Forms custom server controls. 在开发ASP.NET Web Forms自定义服务器控件时遇到了类似的问题。 In order to obtain a reference to the DTE object and create a virtual path to the directory of the file being edited I used the following code inside my custom server control file:
为了获得对DTE对象的引用并创建一个指向正在编辑的文件目录的虚拟路径,我在自定义服务器控制文件中使用了以下代码:
[Bindable(true)]
[Category("Behavior")]
[DefaultValue("")]
[Editor(typeof(System.Web.UI.Design.UrlEditor), typeof(System.Drawing.Design.UITypeEditor))]
public string Url
{
get
{
object urlObject = ViewState["Url"];
if (urlObject == null)
{
if (DesignMode)
{
// Get a reference to the Visual Studio IDE
EnvDTE.DTE dte = this.Site.GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
// Interface for accessing the web application in VS
IWebApplication webApplication = (IWebApplication)this.Site.GetService(typeof(IWebApplication));
// Path of document being edited (Web form in web application)
string activeDocumentPath = dte.ActiveDocument.Path;
// Physical path to the web application root
string projectPath = webApplication.RootProjectItem.PhysicalPath;
// Create virtal path
string relativePath = activeDocumentPath.Replace(projectPath, "~\\");
return relativePath.Replace('\\','/');
}
else
{
return String.Empty;
}
}
else
{
return (string)urlObject;
}
}
set
{
ViewState["Url"] = value;
}
}
This is useful quickly navigating to a file near the one being edited when using the UrlEditor 使用UrlEditor时,这可以快速导航到正在编辑的文件附近的文件
In VS 2010 and 2008, you right click the tab at the top and select "Copy Full Path" from the context menu. 在VS 2010和2008中,右键单击顶部的选项卡,然后从上下文菜单中选择“复制完整路径”。 See my image below.
请参阅下面的图片。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.