Skip to content

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
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/64434-convert-byte-array-to-file-csharp
Open

vladimir-pecanac-main wants to merge 3 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/64434-convert-byte-array-to-file-csharp

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

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/ByteArrayToFile changes.

Fixes:

  • SaveByteArrayToFileWithBinaryWriter opened its file with File.OpenWrite, which uses FileMode.OpenOrCreate and does not truncate. Writing a shorter array over a longer file left the old tail in the file. It now opens with File.Create.
  • The test class was named LiveByteArrayToFileUnitTest, so the CI filter FullyQualifiedName!~Live excluded every test in this folder. It is renamed to ByteArrayToFileUnitTest, and each test now writes to its own temporary file and deletes it afterwards.
  • A regression test writes a 38-byte array and then a 10-byte array to the same path and expects a 10-byte file. On the first commit (before the fix) it fails with expected 10, actual 38; after the second commit it passes.

Additions, one test each:

  • SaveByteArrayToFileAsync (File.WriteAllBytesAsync)
  • AppendByteArrayToFile (File.AppendAllBytes, .NET 9)
  • SaveByteArrayToFileAtOffset (File.OpenHandle plus RandomAccess.Write)

Retarget and packages (NuGet re-queried 2026-10-11, latest listed stable):

  • Both projects: net6.0 to net10.0
  • Microsoft.NET.Test.Sdk 16.11.0 to 18.10.1
  • MSTest.TestAdapter and MSTest.TestFramework 2.2.7 to 4.5.1
  • coverlet.collector 3.1.0 to 10.1.0

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-transitive reports no vulnerable packages.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant