mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-04-04 13:57:37 +00:00
Initial commit
This commit is contained in:
parent
0dbecd11bb
commit
e13b6e59f1
6 changed files with 80 additions and 3 deletions
20
vs4win/Chapter09/WorkingWithRandomAccess/Program.cs
Normal file
20
vs4win/Chapter09/WorkingWithRandomAccess/Program.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
using Microsoft.Win32.SafeHandles; // SafeFileHandle
|
||||
using System.Text; // Encoding
|
||||
|
||||
using SafeFileHandle handle =
|
||||
File.OpenHandle(path: "coffee.txt",
|
||||
mode: FileMode.OpenOrCreate,
|
||||
access: FileAccess.ReadWrite);
|
||||
|
||||
// write to the file
|
||||
string message = "Café £4.39";
|
||||
ReadOnlyMemory<byte> buffer = new(Encoding.UTF8.GetBytes(message));
|
||||
await RandomAccess.WriteAsync(handle, buffer, fileOffset: 0);
|
||||
|
||||
// read from the file
|
||||
long length = RandomAccess.GetLength(handle);
|
||||
Memory<byte> contentBytes = new(new byte[length]);
|
||||
await RandomAccess.ReadAsync(handle, contentBytes, fileOffset: 0);
|
||||
string content = Encoding.UTF8.GetString(contentBytes.ToArray());
|
||||
WriteLine($"Content of file: {content}");
|
||||
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="System.Console" Static="true" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Add table
Add a link
Reference in a new issue