I had a similar problem when developing custom ASP.NET Web Forms controls. To get a link to the DTE object and create a virtual path to the directory of the edited file, I used the following code inside my custom server management file:
[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; } }
It is useful to quickly jump to the file next to the one that is edited when using UrlEditor
source share