Get file path from Visual Studio Editor

I am developing a Visual Studio package written in C #.

How can I get the full path to the active editor programmatically?

+4
source share
4 answers

When working with macros you can use

DTE.ActiveDocument.Path + DTE.ActiveDocument.Name 

to get the full path. This is probably the same in C # when creating VS packages?

+3
source

Here's how you get the full path to a focused (active) document in Visual Studio:

 DTE dte = (DTE)GetService(typeof(DTE)); string document = dte.ActiveDocument.FullName; 
+3
source

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

+2
source

In VS 2010 and 2008, right-click the tab at the top and select "Copy full path" from the context menu. See my image below. alt text

0
source

Source: https://habr.com/ru/post/1333836/


All Articles