Repository: AvaloniaUI/Avalonia
File: src/Avalonia.Base/Platform/Storage/FileIO/StorageProviderHelpers.cs (C#)

Code before:
            {
                return new BclStorageFolder(directory);
            }

            var file = new FileInfo(path);
            if (file.Exists)
            {
                return new BclStorageFile(file);
            }
        }

        return null;
    }

    public static string? TryGetPathFromFileUri(Uri? uri)
    {
        // android "content:", browser and ios relative links are ignored.
        return uri is { IsAbsoluteUri: true, Scheme: "file" } ? uri.LocalPath : null;
    }

    public static Uri UriFromFilePath(string path, bool isDirectory)
    {
        bool isLongPatah = path.StartsWith(@"\\?\");//Windows Long Path Prefix
        if (isLongPatah)
        {
            path = path.Substring(4);
        }
        var uriPath = new StringBuilder(path);
        uriPath = uriPath.Replace("%", $"%{(int)'%':X2}")
                                    .Replace("[", $"%{(int)'[':X2}")
                                    .Replace("]", $"%{(int)']':X2}");
        if (!path.EndsWith('/') && isDirectory)
        {
            uriPath.Append('/');
        }

        return new UriBuilder("file", string.Empty) { Path = uriPath.ToString() }.Uri;
    }

    public static Uri? TryGetUriFromFilePath(string path, bool isDirectory)
    {
        try
        {
            return UriFromFilePath(path, isDirectory);
        }
        catch
        {
            return null;
        }
    }


Review comment (suggestion):
Perf nit: we're already using a `StringBuilder` below, consider using `StringBuilder.Append(path, 4, path.Length - 4)` to avoid an extra string allocation.

Code after:
            {
                return new BclStorageFolder(directory);
            }

            var file = new FileInfo(path);
            if (file.Exists)
            {
                return new BclStorageFile(file);
            }
        }

        return null;
    }

    public static string? TryGetPathFromFileUri(Uri? uri)
    {
        // android "content:", browser and ios relative links are ignored.
        return uri is { IsAbsoluteUri: true, Scheme: "file" } ? uri.LocalPath : null;
    }

    public static Uri UriFromFilePath(string path, bool isDirectory)
    {
        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('/');
        }

        return new UriBuilder("file", string.Empty) { Path = uriPath.ToString() }.Uri;
    }

    public static Uri? TryGetUriFromFilePath(string path, bool isDirectory)
    {
        try
        {
            return UriFromFilePath(path, isDirectory);
        }
        catch
        {