Skip to content

Commit

Permalink
Write file reference with correct capitalisation
Browse files Browse the repository at this point in the history
When dragging a file from Solution Explorer into the Managed Resources editor, the file path is written to the `.resx` file in lower case.

This is because the "drop data" provided by Solution Explorer contains a lower-case path (both for SDK-style and legacy projects).

To fix this, we walk the path, using `Path.EnumerateFileSystemEntries` to produce a correctly cased path for the file being dragged. Unfortunately there's no better way I could find to achieve this. I tried quite a few different approaches and this seemed like the least rubbish. The approach used in `GetFileNameInActualCase` elsewhere in the resource editor only handles the file name, not any directories above it.
  • Loading branch information
drewnoakes committed Mar 2, 2023
1 parent 436bc57 commit 7cb711c
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3053,7 +3053,7 @@ Namespace Microsoft.VisualStudio.Editors.ResourceEditor
With Info
.Guid = SplitInfo(0)
.ProjectFile = SplitInfo(1)
.FilePath = SplitInfo(2)
.FilePath = GetExactPath(SplitInfo(2))
End With
Files(i) = Info
Next
Expand All @@ -3069,6 +3069,30 @@ Namespace Microsoft.VisualStudio.Editors.ResourceEditor
Return Files
End Function

' Converts the provided path to use the same capitalization as the file system. Files dragged
' from Solution Explorer produce drop data having a lower-case path, which we then write to
' in the .resx file. Windows has a case-insensitive file system, but users on other operation
' systems (such as Linux) can hit run-time errors when the case doesn't match. This avoids that.
Private Shared Function GetExactPath(path As String) As String
If path Is Nothing OrElse (Not File.Exists(path) AndAlso Not Directory.Exists(path)) Then
Return path
End If

Try
Dim root = System.IO.Path.GetPathRoot(path)
Dim parts = path.Substring(root.Length).Split(System.IO.Path.DirectorySeparatorChar, System.IO.Path.AltDirectorySeparatorChar)

For Each part As String In parts
root = Directory.EnumerateFileSystemEntries(root, part).First()
Next

Return root
Catch
' If we hit any issues, just return the path unchanged
Return path
End Try
End Function

#End Region

#End Region
Expand Down

0 comments on commit 7cb711c

Please sign in to comment.