From e13b6e59f1a0bebce61fa0c328854baa815f415d Mon Sep 17 00:00:00 2001 From: Mark J Price Date: Mon, 14 Mar 2022 08:44:08 +0000 Subject: [PATCH] Initial commit --- vs4win/Chapter09/Chapter09.sln | 12 ++++++++--- .../WorkingWithRandomAccess/Program.cs | 20 +++++++++++++++++++ .../WorkingWithRandomAccess.csproj | 14 +++++++++++++ vscode/Chapter09/Chapter09.code-workspace | 3 +++ .../WorkingWithRandomAccess/Program.cs | 20 +++++++++++++++++++ .../WorkingWithRandomAccess.csproj | 14 +++++++++++++ 6 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 vs4win/Chapter09/WorkingWithRandomAccess/Program.cs create mode 100644 vs4win/Chapter09/WorkingWithRandomAccess/WorkingWithRandomAccess.csproj create mode 100644 vscode/Chapter09/WorkingWithRandomAccess/Program.cs create mode 100644 vscode/Chapter09/WorkingWithRandomAccess/WorkingWithRandomAccess.csproj diff --git a/vs4win/Chapter09/Chapter09.sln b/vs4win/Chapter09/Chapter09.sln index f39aaac..9c3f4f5 100644 --- a/vs4win/Chapter09/Chapter09.sln +++ b/vs4win/Chapter09/Chapter09.sln @@ -9,11 +9,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorkingWithStreams", "Worki EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorkingWithEncodings", "WorkingWithEncodings\WorkingWithEncodings.csproj", "{70A9750E-5C55-4879-B601-20AC5F6CDCB4}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkingWithSerialization", "WorkingWithSerialization\WorkingWithSerialization.csproj", "{7493AA42-910F-4FB2-B3D4-34553255BD54}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorkingWithSerialization", "WorkingWithSerialization\WorkingWithSerialization.csproj", "{7493AA42-910F-4FB2-B3D4-34553255BD54}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkingWithJson", "WorkingWithJson\WorkingWithJson.csproj", "{CA13AF96-7EE4-42C4-A671-C373E218726A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorkingWithJson", "WorkingWithJson\WorkingWithJson.csproj", "{CA13AF96-7EE4-42C4-A671-C373E218726A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch09Ex02SerializingShapes", "Ch09Ex02SerializingShapes\Ch09Ex02SerializingShapes.csproj", "{27FA94CC-1856-4512-9D09-0214CF846311}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ch09Ex02SerializingShapes", "Ch09Ex02SerializingShapes\Ch09Ex02SerializingShapes.csproj", "{27FA94CC-1856-4512-9D09-0214CF846311}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkingWithRandomAccess", "WorkingWithRandomAccess\WorkingWithRandomAccess.csproj", "{EFC99F8A-CB39-48BB-A70D-5D7DB0A001D6}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {27FA94CC-1856-4512-9D09-0214CF846311}.Debug|Any CPU.Build.0 = Debug|Any CPU {27FA94CC-1856-4512-9D09-0214CF846311}.Release|Any CPU.ActiveCfg = Release|Any CPU {27FA94CC-1856-4512-9D09-0214CF846311}.Release|Any CPU.Build.0 = Release|Any CPU + {EFC99F8A-CB39-48BB-A70D-5D7DB0A001D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EFC99F8A-CB39-48BB-A70D-5D7DB0A001D6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EFC99F8A-CB39-48BB-A70D-5D7DB0A001D6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EFC99F8A-CB39-48BB-A70D-5D7DB0A001D6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/vs4win/Chapter09/WorkingWithRandomAccess/Program.cs b/vs4win/Chapter09/WorkingWithRandomAccess/Program.cs new file mode 100644 index 0000000..94c781c --- /dev/null +++ b/vs4win/Chapter09/WorkingWithRandomAccess/Program.cs @@ -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 buffer = new(Encoding.UTF8.GetBytes(message)); +await RandomAccess.WriteAsync(handle, buffer, fileOffset: 0); + +// read from the file +long length = RandomAccess.GetLength(handle); +Memory contentBytes = new(new byte[length]); +await RandomAccess.ReadAsync(handle, contentBytes, fileOffset: 0); +string content = Encoding.UTF8.GetString(contentBytes.ToArray()); +WriteLine($"Content of file: {content}"); + diff --git a/vs4win/Chapter09/WorkingWithRandomAccess/WorkingWithRandomAccess.csproj b/vs4win/Chapter09/WorkingWithRandomAccess/WorkingWithRandomAccess.csproj new file mode 100644 index 0000000..cd63b28 --- /dev/null +++ b/vs4win/Chapter09/WorkingWithRandomAccess/WorkingWithRandomAccess.csproj @@ -0,0 +1,14 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + diff --git a/vscode/Chapter09/Chapter09.code-workspace b/vscode/Chapter09/Chapter09.code-workspace index 05f31d5..e126e57 100644 --- a/vscode/Chapter09/Chapter09.code-workspace +++ b/vscode/Chapter09/Chapter09.code-workspace @@ -9,6 +9,9 @@ { "path": "WorkingWithEncodings" }, + { + "path": "WorkingWithRandomAccess" + }, { "path": "WorkingWithSerialization" }, diff --git a/vscode/Chapter09/WorkingWithRandomAccess/Program.cs b/vscode/Chapter09/WorkingWithRandomAccess/Program.cs new file mode 100644 index 0000000..94c781c --- /dev/null +++ b/vscode/Chapter09/WorkingWithRandomAccess/Program.cs @@ -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 buffer = new(Encoding.UTF8.GetBytes(message)); +await RandomAccess.WriteAsync(handle, buffer, fileOffset: 0); + +// read from the file +long length = RandomAccess.GetLength(handle); +Memory contentBytes = new(new byte[length]); +await RandomAccess.ReadAsync(handle, contentBytes, fileOffset: 0); +string content = Encoding.UTF8.GetString(contentBytes.ToArray()); +WriteLine($"Content of file: {content}"); + diff --git a/vscode/Chapter09/WorkingWithRandomAccess/WorkingWithRandomAccess.csproj b/vscode/Chapter09/WorkingWithRandomAccess/WorkingWithRandomAccess.csproj new file mode 100644 index 0000000..cd63b28 --- /dev/null +++ b/vscode/Chapter09/WorkingWithRandomAccess/WorkingWithRandomAccess.csproj @@ -0,0 +1,14 @@ + + + + Exe + net7.0 + enable + enable + + + + + + +