我正在开发一个用C#编写的Visual Studio扩展程序。
如何通过编程方式获取活动编辑器的完整路径?
我正在开发一个用C#编写的Visual Studio扩展程序。
如何通过编程方式获取活动编辑器的完整路径?
DTE dte = (DTE)GetService(typeof(DTE));
string document = dte.ActiveDocument.FullName;
在处理宏时,您可以使用以下方法:
DTE.ActiveDocument.Path + DTE.ActiveDocument.Name
获取完整路径。在制作VS扩展时,可能与C#相同?
在开发ASP.NET Web Forms自定义服务器控件时,我遇到了类似的问题。为了获得对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;
}
}
使用UrlEditor时,这是快速导航到正在编辑的文件附近的有用方法。