Handle long file paths in UriFromFilePath (#19955)
* fix long path * Update UriExtensionsTests.cs * Update StorageProviderHelpers.cs
This commit is contained in:
@@ -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('/');
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user