在 VisualStudioIDE 中显示解决方案/文件路径

我经常处理 VisualStudio 的多个实例,经常处理同一解决方案的不同分支。

Visual C + + 6.0 用于在标题栏中显示当前源文件的完整路径,但 Visual Studio 2005似乎没有这样做。这使得找出我当前正在查看的解决方案的哪个分支变得有些尴尬(据我所知,最快的方法是将鼠标悬停在一个选项卡上,这样您就可以获得源文件的路径作为工具提示)。

有没有一种方法可以将完整的解决方案或文件路径放入标题栏,或者至少放在总是可见的地方,这样我就可以快速判断哪个分支被加载到每个实例中?

46571 次浏览

There is not a native way to do it, but you can achieve it with a macro. The details are described here in full: How To Show Full File Path (or Anything Else) in VS 2005 Title Bar

You just have to add a little Visual Basic macro to the EvironmentEvents macro section and restart Visual Studio.

Note: The path will not show up when you first load Visual Studio, but it will whenever you change which file you are viewing. There is probably a way to fix this, but it doesn't seem like a big deal.

Related note: As an alternative, for Visual Studio 2005 you can use the command menu FileAdvanced Save Options. The dialog displays the full path of the current file, and you are able to copy the text.

It's awkward indeed. Hovering on the tab is indeed one of the few things useful.

Alternative: right click on the file tab: Find your File Path in Visual Studio. It seems we have to do with that.

Check out the latest release of VSCommands 2010 Lite. It introduced a feature called Friendly Solution Name where you can set it to display the solution file path (or any part of it) in Visual Studio's main window title.

More details: http://vscommands.com/releasenotes/3.6.8.0 and http://vscommands.com/releasenotes/3.6.9.0

For Visual Studio 2008, a slightly better way to write the macro from the accepted answer is to use the Solution events instead of the document ones - this lets you always edit the title bar, even if you don't have a document selected.

Here's the macro my coworker and I put together based on the other one - you'll want to change lines 15-18 to pull your branch name from the source directory for however you're set up.

Private timer As System.Threading.Timer


Declare Auto Function SetWindowText Lib "user32" (ByVal hWnd As System.IntPtr, ByVal lpstring As String) As Boolean


Private _branchName As String = String.Empty


Private Sub SolutionEvents_Opened() Handles SolutionEvents.Opened
Try
If timer Is Nothing Then
' Create timer which refreshes the caption because
' IDE resets the caption very often
Dim autoEvent As New System.Threading.AutoResetEvent(False)
Dim timerDelegate As System.Threading.TimerCallback = _
AddressOf tick
timer = New System.Threading.Timer(timerDelegate, autoEvent, 0, 25)
End If
Dim sourceIndex As Integer = DTE.Solution.FullName.IndexOf("\Source")
Dim shortTitle As String = DTE.Solution.FullName.Substring(0, sourceIndex)
Dim lastIndex As Integer = shortTitle.LastIndexOf("\")
_branchName = shortTitle.Substring(lastIndex + 1)
showTitle(_branchName)
Catch ex As Exception


End Try
End Sub




Private Sub SolutionEvents_BeforeClosing() Handles SolutionEvents.BeforeClosing
If Not timer Is Nothing Then
timer.Dispose()
End If
End Sub




''' <summary>Dispose the timer on IDE shutdown.</summary>
Public Sub DTEEvents_OnBeginShutdown() Handles DTEEvents.OnBeginShutdown
If Not timer Is Nothing Then
timer.Dispose()
End If
End Sub




'''<summary>Called by timer.</summary>
Public Sub tick(ByVal state As Object)
Try
showTitle(_branchName)
Catch ex As System.Exception
End Try
End Sub




'''<summary>Shows the title in main window.</summary>
Private Sub showTitle(ByVal title As String)
SetWindowText(New System.IntPtr(DTE.MainWindow.HWnd), title & " - " & DTE.Name)
End Sub

This is a extension available in the online gallery specifically tailored for this job. Checkout Labs > Visual Studio Extension: Customize Visual Studio Window Title.

Use the MKLINK command to create a link to your existing solution. As far as Visual Studio is concerned, it's working with the link file, but any changes go to the underlying .sln file.

I wrote a blog entry here about it...

http://willissoftware.com/?p=72

For the people that didn't get the VB method to work (like me) you can use a plugin:

Customize Visual Studio Window Title

It was tested it in Visual Studio 2008 Ultimate. You can configure it in the Options menu of Visual Studio.

I am using VSCommands 10 to show the full path of the solution file open.

Friendly Name: {repo}
Solution Path Regex: (?<repo>.*)

Now my main title window looks like this:

c:\repositories\acme.marketplace.trunk\Acme.Marketplace.web\Acme.Marketplace.Web.sln

I can quickly glance and see that I am working in the trunk folder or a rc folder because we use Mercurial (Hg) and keep separate folders for trunk, rc, preprod, prod like this:

c:\repositories\acme.marketplace.rc1
c:\repositories\acme.marketplace.rc2
c:\repositories\acme.marketplace.trunk
c:\repositories\acme.marketplace.preprod
c:\repositories\acme.marketplace.prod

If you are using Visual Studio 2010 or above you can you the extension "Visual Studio Window Title Changer".

Install this and use the following 'Window Title Setup' expression to display the solution path:

'sln_dir + "/" + orig_title'

Use the extension manager to download and install the extension. Details of the extension and how to use it can be found here:

https://visualstudiogallery.msdn.microsoft.com/2e8ebfe4-023f-4c4d-9b7a-d05bbc5cb239?SRC=VSIDE

How to customise the Visual Studio window title

Install the Customize Visual Studio Window Title plugin.

After installing the extension, the settings can be found in the menu.

Menu ToolsOptionsCustomize VS Window Title.

More information

Customize Visual Studio Window Title is a lightweight extension to Visual Studio, which allows you to change the window title to include a folder tree:

Enter image description here

Features

  • A configurable minimum and maximum depth distance from the solution/project file
  • Allows the use of special tags to help with many other possible scenarios, which include Git, Mercurial, and TFS.

As Dan also mentioned it in a comment, the File Path On Footer extension serves the same purpose.

TabsStudio | US$49

It is a pretty good (although paid) Visual Studio extension that provides:

  • Tab grouping
  • Tab coloring
  • Title transformation
  • Lots of customization and extensions

Tabs Studio Screenshot

File Path On Footer | Free

It displays the full file path on the bottom of the editor window:

File Path On Footer Screenshot

Honorable Mention: Visual Studio Code

Visual Studio Code version 1.26 implemented breadcrumbs which displays the file path in a separate row at the top of the editor window when using tabs or inline the file name when in its own window.

Visual Studio Code Breadcrumbs Screenshot

File > Preferences > Settings >> Window:Title

I just changed ${activeEditorShort} => ${activeEditorLong}

within the setting: ${dirty}${activeEditorLong}${separator}${rootName}${separator}${appName}

Worked immediately when I clicked a file.

Great help right in the setting ...

Window: Title -- Controls the window title based on the active editor. Variables are substituted based on the context:

${activeEditorShort}: the file name (e.g. myFile.txt).

${activeEditorMedium}: the path of the file relative to the workspace folder (e.g. myFolder/myFileFolder/myFile.txt).

...

Visual Studio Code Version: 1.56.2 Date: 2021-05-12

I found one reference saying this existed since 2017.