diff --git a/src/Essentials/src/FileSystem/FileSystem.windows.cs b/src/Essentials/src/FileSystem/FileSystem.windows.cs index 54f480cf008f..d3484157da85 100644 --- a/src/Essentials/src/FileSystem/FileSystem.windows.cs +++ b/src/Essentials/src/FileSystem/FileSystem.windows.cs @@ -101,8 +101,15 @@ void PlatformInit(FileBase file) // we can't do anything here, but Windows will take care of it string PlatformGetContentType(string extension) => null; - internal virtual Task PlatformOpenReadAsync() => - File.OpenStreamForReadAsync(); + internal async virtual Task PlatformOpenReadAsync() + { + if (File is null && FullPath is not null) + { + File = await StorageFile.GetFileFromPathAsync(FullPath); + } + + return await File.OpenStreamForReadAsync(); + } } public partial class FileResult diff --git a/src/Essentials/test/DeviceTests/Tests/FileSystem_Tests.cs b/src/Essentials/test/DeviceTests/Tests/FileSystem_Tests.cs index 05b0c1d5d513..90417c1cc279 100644 --- a/src/Essentials/test/DeviceTests/Tests/FileSystem_Tests.cs +++ b/src/Essentials/test/DeviceTests/Tests/FileSystem_Tests.cs @@ -57,5 +57,49 @@ public async Task ValidateMIMEFormat() File.Delete(filePath); } #endif + [Fact] + public async Task CheckFileResultWithFilePath() + { + string filePath = Path.Combine(FileSystem.CacheDirectory, "sample.txt"); + await File.WriteAllTextAsync(filePath, "Sample content for testing"); + + var fileResult = new FileResult(filePath); + + using var stream = await fileResult.OpenReadAsync(); + + Assert.NotNull(stream); + + File.Delete(filePath); + } + + [Fact] + public async Task CheckFileResultOpenReadAsyncMultipleTimes() + { + string filePath = Path.Combine(FileSystem.CacheDirectory, "sample_multiple.txt"); + string expectedContent = "Sample content for multiple stream testing"; + await File.WriteAllTextAsync(filePath, expectedContent); + + var fileResult = new FileResult(filePath); + + // First call to OpenReadAsync + using (var firstStream = await fileResult.OpenReadAsync()) + { + Assert.NotNull(firstStream); + using var firstReader = new StreamReader(firstStream); + var firstContent = await firstReader.ReadToEndAsync(); + Assert.Equal(expectedContent, firstContent); + } + + // Second call to OpenReadAsync - should still work + using (var secondStream = await fileResult.OpenReadAsync()) + { + Assert.NotNull(secondStream); + using var secondReader = new StreamReader(secondStream); + var secondContent = await secondReader.ReadToEndAsync(); + Assert.Equal(expectedContent, secondContent); + } + + File.Delete(filePath); + } } }