diff --git a/src/Avalonia.Base/Platform/Storage/FileIO/StorageProviderHelpers.cs b/src/Avalonia.Base/Platform/Storage/FileIO/StorageProviderHelpers.cs index 989ec23a29..16ac8e87d9 100644 --- a/src/Avalonia.Base/Platform/Storage/FileIO/StorageProviderHelpers.cs +++ b/src/Avalonia.Base/Platform/Storage/FileIO/StorageProviderHelpers.cs @@ -37,11 +37,19 @@ internal static class StorageProviderHelpers public static Uri UriFromFilePath(string path, bool isDirectory) { - var uriPath = new StringBuilder(path) - .Replace("%", $"%{(int)'%':X2}") - .Replace("[", $"%{(int)'[':X2}") - .Replace("]", $"%{(int)']':X2}"); - + var uriPath = new StringBuilder(); + bool isLongPath = path.StartsWith(@"\\?\", StringComparison.Ordinal);//Windows long path prefix + if (isLongPath) + { + uriPath.Append(path, 4, path.Length - 4); + } + else + { + uriPath.Append(path); + } + uriPath = uriPath.Replace("%", $"%{(int)'%':X2}") + .Replace("[", $"%{(int)'[':X2}") + .Replace("]", $"%{(int)']':X2}"); if (!path.EndsWith('/') && isDirectory) { uriPath.Append('/'); diff --git a/tests/Avalonia.Base.UnitTests/Utilities/UriExtensionsTests.cs b/tests/Avalonia.Base.UnitTests/Utilities/UriExtensionsTests.cs index 83380bf6a0..e74851ef09 100644 --- a/tests/Avalonia.Base.UnitTests/Utilities/UriExtensionsTests.cs +++ b/tests/Avalonia.Base.UnitTests/Utilities/UriExtensionsTests.cs @@ -40,4 +40,15 @@ public class UriExtensionsTests Assert.Equal(path, uri.LocalPath); } + + [Theory] + + [InlineData(@"\\?\D:\abcdefgh\abcdefgh\abcdefabcdefgh\abcdefghabcdefghabcdefgha\bcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh\abcdefghabcdefghabcdefgha\bcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh", + @"D:\abcdefgh\abcdefgh\abcdefabcdefgh\abcdefghabcdefghabcdefgha\bcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh\abcdefghabcdefghabcdefgha\bcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh")] + public void Should_Convert_Long_File_Path_To_Uri_And_Back(string prepath,string path) + { + var uri = StorageProviderHelpers.UriFromFilePath(prepath, false); + + Assert.Equal(path, uri.LocalPath); + } }