Repository navigation
Convert Byte Array to File: net10.0, File.Create fix, CI runs the tests - #2284
Open
vladimir-pecanac-main wants to merge 3 commits into
Open
vladimir-pecanac-main wants to merge 3 commits into
vladimir-pecanac-main wants to merge 3 commits into
Conversation
Both projects move from net6.0 to net10.0. Microsoft.NET.Test.Sdk 18.10.1, MSTest.TestAdapter and MSTest.TestFramework 4.5.1, coverlet.collector 10.1.0. The test class was named LiveByteArrayToFileUnitTest, and the CI filter FullyQualifiedName!~Live excluded every test in the folder. It is renamed to ByteArrayToFileUnitTest. Each test now writes to its own temporary file and deletes it afterwards. Adds a regression test that writes a 38-byte array and then a 10-byte array through SaveByteArrayToFileWithBinaryWriter and expects a 10-byte file. It fails here, because File.OpenWrite does not truncate an existing file.
File.OpenWrite opens with FileMode.OpenOrCreate, which keeps the old length: writing a shorter array over a longer file left the old tail behind. File.Create truncates an existing file. The regression test now passes.
SaveByteArrayToFileAsync wraps File.WriteAllBytesAsync, AppendByteArrayToFile wraps File.AppendAllBytes (.NET 9), and SaveByteArrayToFileAtOffset opens a handle with File.OpenHandle and writes with RandomAccess.Write. Each has a test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sample update for the article "Convert Byte Array to File in C#" (https://code-maze.com/convert-byte-array-to-file-csharp/). Only
files-csharp/ByteArrayToFilechanges.Fixes:
SaveByteArrayToFileWithBinaryWriteropened its file withFile.OpenWrite, which usesFileMode.OpenOrCreateand does not truncate. Writing a shorter array over a longer file left the old tail in the file. It now opens withFile.Create.LiveByteArrayToFileUnitTest, so the CI filterFullyQualifiedName!~Liveexcluded every test in this folder. It is renamed toByteArrayToFileUnitTest, and each test now writes to its own temporary file and deletes it afterwards.Additions, one test each:
SaveByteArrayToFileAsync(File.WriteAllBytesAsync)AppendByteArrayToFile(File.AppendAllBytes, .NET 9)SaveByteArrayToFileAtOffset(File.OpenHandleplusRandomAccess.Write)Retarget and packages (NuGet re-queried 2026-10-11, latest listed stable):
Local run on SDK 10.0.302, runtime 10.0.10: build 0 warnings, 0 errors;
dotnet test --filter "FullyQualifiedName!~Live"passes 7 of 7;dotnet list package --vulnerable --include-transitivereports no vulnerable packages.