VisualStudio: 将解决方案资源管理器滚动到当前文件的快捷方式

在解决方案资源管理器中,选择 自然而然地跟踪当前文件,我是 我没问。这已经在 这个问题中得到了回答,我已经关闭了这个选项,因为我讨厌这种行为。

我想有一个快捷方式(或宏,或... .)跳转到解决方案资源管理器中当前正在编辑的文件。

38653 次浏览

As far as I know there is no such option before VS 2012.

In VS 2012 the "Sync with Active Document" option was introduced. You can find description and screen on this blog (scroll to "Sync with Active Document" in the middle of page).

For VS2010 I found this macro and works for me :

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90


Public Module Utilities
Public Sub TrackProjectItem()
Dim solution As Solution2 = DTE.Solution
If Not solution.IsOpen OrElse DTE.ActiveDocument Is Nothing Then Return


solution.FindProjectItem(DTE.ActiveDocument.FullName).ExpandView()


Dim FileName As String = DTE.ActiveDocument.FullName


Dim SolutionExplorerPath As String
Dim items As EnvDTE.UIHierarchyItems = DTE.ToolWindows.SolutionExplorer.UIHierarchyItems
Dim item As Object = FindItem(items, FileName, SolutionExplorerPath)


If item Is Nothing Then
MsgBox("Couldn't find the item in Solution Explorer.")
Return
End If


DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()
DTE.ActiveWindow.Object.GetItem(SolutionExplorerPath).Select(vsUISelectionType.vsUISelectionTypeSelect)
End Sub


Public Function FindItem(ByVal Children As UIHierarchyItems, ByVal FileName As String, ByRef SolutionExplorerPath As String) As Object
For Each CurrentItem As UIHierarchyItem In Children
Dim TypeName As String = Microsoft.VisualBasic.Information.TypeName(CurrentItem.Object)
If TypeName = "ProjectItem" Then
Dim projectitem As EnvDTE.ProjectItem = CType(CurrentItem.Object, EnvDTE.ProjectItem)
Dim i As Integer = 1
While i <= projectitem.FileCount
If projectitem.FileNames(i) = FileName Then
SolutionExplorerPath = CurrentItem.Name
Return CurrentItem
End If
i = i + 1
End While
End If


Dim ChildItem As UIHierarchyItem = FindItem(CurrentItem.UIHierarchyItems, FileName, SolutionExplorerPath)
If Not ChildItem Is Nothing Then
SolutionExplorerPath = CurrentItem.Name + "\" + SolutionExplorerPath
Return ChildItem
End If
Next
End Function
End Module

Original Source here

In Visual Studio 2010/2012 you can use this extension (link). It adds option to sync on Solution Explorer toolbar and code context menu.

In VS 2013 there is a built-in keyboard shortcut (CTRL+\, S)

  1. Press CTRL+backslash
  2. Release both keys
  3. Press the S key

Or click the button highlighted in the image bellow.

Sync with active document

One also has the option to customize the keyboard shortcut if you don't like the default combination :)

In Visual Studio 2015, 2017, 2019 and 2022 you can press Ctrl + [ and then s.

This will highlight the file currently being edited in Solution Explorer.

This can be configured via the following keyboard command: SolutionExplorer.SyncWithActiveDocument

To reconfigure, navigate to Tools -> Options -> Environment -> Keyboard

On my keyboard I had to press:

Ctrl + ` + S

Do note that the sign in the middle is the key just left of the backspace.

Using Visual Studio 2015.

To locate the file you're currently editing in Solution Explorer:

Ctrl + W + S

I previously used Shift + Alt + L, but for some reason this is no longer working.

The other suggestions (Ctrl+\,S and Ctrl+[,S and Ctrl + ` + S) do not work for me in VS2015. I don't use resharper and dont like using macros when simple shortcuts are available.

In Visual Studio 2015, with ReSharper, I am able to press Shift + Alt + L to highlight the current file being edited in Solution Explorer.

If I got your question right, you can go to Tools -> Options -> Projects and Solutions -> General and check the "Track Active Item in Solution Explorer" option.

For VS 2017 the default configuration is:

CTRL + [,S

And the complete list of shortcuts you can find here:

http://visualstudioshortcuts.com/2017/