Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Essentials/src/FileSystem/FileSystem.windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stream> PlatformOpenReadAsync() =>
File.OpenStreamForReadAsync();
internal async virtual Task<Stream> PlatformOpenReadAsync()
{
if (File is null && FullPath is not null)
{
File = await StorageFile.GetFileFromPathAsync(FullPath);
}

return await File.OpenStreamForReadAsync();
}
}

public partial class FileResult
Expand Down
44 changes: 44 additions & 0 deletions src/Essentials/test/DeviceTests/Tests/FileSystem_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,49 @@ public async Task ValidateMIMEFormat()
File.Delete(filePath);
}
#endif
[Fact]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add a test that calls OpenReadAsync twice to validate it? Ensures the second call does not regress and that the StorageFile remains usable across multiple streams.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsuarezruiz , Based on suggestion, I've modified the test

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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L55 (using var stream = await fileResult.OpenReadAsync();) is still using the file..?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MartyIX,
It is using FileResult, not the file directly. The OpenReadAsync() method returns a stream from the FileResult

Copy link
Copy Markdown
Contributor

@MartyIX MartyIX Mar 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know this API in depth. It's just I find it weird that you seem to operate on a file and delete it before the stream is disposed.

I would expect:

using (var stream = await fileResult.OpenReadAsync())
{
  Assert.NotNull(stream);
}

File.Delete(filePath);

I find it safer. But I might be very well wrong, so feel free to ignore this suggestion.

edit: Ah, I see what you are saying.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,
does now the FileResult is able to load the file correctly ? i'm in maui net 8
Thank's

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@robitsrl , Yes, in .NET 8, FileResult is able to load the file correctly when creating a new FileResult using an existing FileResult.

Comment on lines +66 to +72
Copy link

Copilot AI Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file cleanup could fail if an exception occurs during the test. Consider wrapping the test logic in a try-finally block or moving the cleanup to a finally block to ensure the test file is deleted even if assertions fail.

Suggested change
var fileResult = new FileResult(filePath);
using var stream = await fileResult.OpenReadAsync();
Assert.NotNull(stream);
File.Delete(filePath);
try
{
var fileResult = new FileResult(filePath);
using var stream = await fileResult.OpenReadAsync();
Assert.NotNull(stream);
}
finally
{
File.Delete(filePath);
}

Copilot uses AI. Check for mistakes.
}

[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);
Comment on lines +78 to +102
Copy link

Copilot AI Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file cleanup could fail if an exception occurs during the test. Consider wrapping the test logic in a try-finally block or moving the cleanup to a finally block to ensure the test file is deleted even if assertions fail.

Copilot uses AI. Check for mistakes.
}
}
}
Loading