Skip to content

Commit

Permalink
Chapter 20: File System Data
Browse files Browse the repository at this point in the history
  • Loading branch information
csharpguitar committed Dec 22, 2017
1 parent 48afa56 commit 8d9d5b4
Show file tree
Hide file tree
Showing 38 changed files with 1,378 additions and 0 deletions.
44 changes: 44 additions & 0 deletions BeginningCSharp7/BeginningCSharp7.sln
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,20 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch19Ex01", "..\Chapter19\Ch
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch19Ex02", "..\Chapter19\Ch19Ex02\Ch19Ex02.csproj", "{A074009B-D7DD-429F-933F-37D1FE1676C5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Chapter20", "Chapter20", "{37074111-77F8-42CE-99C4-B427CA07EFA4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Compressor", "..\Chapter20\Compressor\Compressor\Compressor.csproj", "{A3994BE8-0B47-4522-9362-20B3861C2FD5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileWatch", "..\Chapter20\FileWatch\FileWatch\FileWatch.csproj", "{8996731A-8EEE-4B6F-AABA-1B76ECB47081}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReadFile", "..\Chapter20\ReadFile\ReadFile\ReadFile.csproj", "{22074A8D-FC21-4CEF-BFA7-BC8E1269D04C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StreamRead", "..\Chapter20\StreamRead\StreamRead\StreamRead.csproj", "{9243879D-5C4B-4F26-BED0-4409B4F99773}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StreamWrite", "..\Chapter20\StreamWrite\StreamWrite\StreamWrite.csproj", "{FD758EDE-1C59-4F8F-9B74-A2FB0C747032}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WriteFile", "..\Chapter20\WriteFile\WriteFile\WriteFile.csproj", "{F7F41C4D-B388-45EA-A230-4ED29A28208C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -545,6 +559,30 @@ Global
{A074009B-D7DD-429F-933F-37D1FE1676C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A074009B-D7DD-429F-933F-37D1FE1676C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A074009B-D7DD-429F-933F-37D1FE1676C5}.Release|Any CPU.Build.0 = Release|Any CPU
{A3994BE8-0B47-4522-9362-20B3861C2FD5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A3994BE8-0B47-4522-9362-20B3861C2FD5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3994BE8-0B47-4522-9362-20B3861C2FD5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3994BE8-0B47-4522-9362-20B3861C2FD5}.Release|Any CPU.Build.0 = Release|Any CPU
{8996731A-8EEE-4B6F-AABA-1B76ECB47081}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8996731A-8EEE-4B6F-AABA-1B76ECB47081}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8996731A-8EEE-4B6F-AABA-1B76ECB47081}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8996731A-8EEE-4B6F-AABA-1B76ECB47081}.Release|Any CPU.Build.0 = Release|Any CPU
{22074A8D-FC21-4CEF-BFA7-BC8E1269D04C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{22074A8D-FC21-4CEF-BFA7-BC8E1269D04C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{22074A8D-FC21-4CEF-BFA7-BC8E1269D04C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{22074A8D-FC21-4CEF-BFA7-BC8E1269D04C}.Release|Any CPU.Build.0 = Release|Any CPU
{9243879D-5C4B-4F26-BED0-4409B4F99773}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9243879D-5C4B-4F26-BED0-4409B4F99773}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9243879D-5C4B-4F26-BED0-4409B4F99773}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9243879D-5C4B-4F26-BED0-4409B4F99773}.Release|Any CPU.Build.0 = Release|Any CPU
{FD758EDE-1C59-4F8F-9B74-A2FB0C747032}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FD758EDE-1C59-4F8F-9B74-A2FB0C747032}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FD758EDE-1C59-4F8F-9B74-A2FB0C747032}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FD758EDE-1C59-4F8F-9B74-A2FB0C747032}.Release|Any CPU.Build.0 = Release|Any CPU
{F7F41C4D-B388-45EA-A230-4ED29A28208C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F7F41C4D-B388-45EA-A230-4ED29A28208C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F7F41C4D-B388-45EA-A230-4ED29A28208C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F7F41C4D-B388-45EA-A230-4ED29A28208C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -629,6 +667,12 @@ Global
{0254A0AD-DABA-4546-9E55-B9722D427337} = {DAC80B8B-41F1-4689-98C4-0F90C98A47A4}
{E027BD99-6C10-4062-AF35-3D7FBA5163B4} = {6181A4AF-C437-4C23-86F2-3D309E67CEEB}
{A074009B-D7DD-429F-933F-37D1FE1676C5} = {6181A4AF-C437-4C23-86F2-3D309E67CEEB}
{A3994BE8-0B47-4522-9362-20B3861C2FD5} = {37074111-77F8-42CE-99C4-B427CA07EFA4}
{8996731A-8EEE-4B6F-AABA-1B76ECB47081} = {37074111-77F8-42CE-99C4-B427CA07EFA4}
{22074A8D-FC21-4CEF-BFA7-BC8E1269D04C} = {37074111-77F8-42CE-99C4-B427CA07EFA4}
{9243879D-5C4B-4F26-BED0-4409B4F99773} = {37074111-77F8-42CE-99C4-B427CA07EFA4}
{FD758EDE-1C59-4F8F-9B74-A2FB0C747032} = {37074111-77F8-42CE-99C4-B427CA07EFA4}
{F7F41C4D-B388-45EA-A230-4ED29A28208C} = {37074111-77F8-42CE-99C4-B427CA07EFA4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F596D385-8F2C-409A-B4F9-A815F1E00878}
Expand Down
22 changes: 22 additions & 0 deletions Chapter20/Compressor/Compressor.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.15
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Compressor", "Compressor\Compressor.csproj", "{A3994BE8-0B47-4522-9362-20B3861C2FD5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A3994BE8-0B47-4522-9362-20B3861C2FD5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A3994BE8-0B47-4522-9362-20B3861C2FD5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3994BE8-0B47-4522-9362-20B3861C2FD5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3994BE8-0B47-4522-9362-20B3861C2FD5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions Chapter20/Compressor/Compressor/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1"/>
</startup>
</configuration>
53 changes: 53 additions & 0 deletions Chapter20/Compressor/Compressor/Compressor.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{A3994BE8-0B47-4522-9362-20B3861C2FD5}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Compressor</RootNamespace>
<AssemblyName>Compressor</AssemblyName>
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
76 changes: 76 additions & 0 deletions Chapter20/Compressor/Compressor/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Compression;
using static System.Console;

namespace Compressor
{
class Program
{
static void SaveCompressedFile(string filename, string data)
{
FileStream fileStream =
new FileStream(filename, FileMode.Create, FileAccess.Write);
GZipStream compressionStream =
new GZipStream(fileStream, CompressionMode.Compress);
StreamWriter writer = new StreamWriter(compressionStream);
writer.Write(data);
writer.Close();
}

static string LoadCompressedFile(string filename)
{
FileStream fileStream =
new FileStream(filename, FileMode.Open, FileAccess.Read);
GZipStream compressionStream =
new GZipStream(fileStream, CompressionMode.Decompress);
StreamReader reader = new StreamReader(compressionStream);
string data = reader.ReadToEnd();
reader.Close();
return data;
}

static void Main(string[] args)
{
try
{
string filename = "compressedFile.txt";

WriteLine(
"Enter a string to compress (will be repeated 100 times):");
string sourceString = ReadLine();
StringBuilder sourceStringMultiplier =
new StringBuilder(sourceString.Length * 100);
for (int i = 0; i < 100; i++)
{
sourceStringMultiplier.Append(sourceString);
}
sourceString = sourceStringMultiplier.ToString();
WriteLine($"Source data is {sourceString.Length} bytes long.");

SaveCompressedFile(filename, sourceString);
WriteLine($"\nData saved to {filename}.");

FileInfo compressedFileData = new FileInfo(filename);
WriteLine($"Compressed file is {compressedFileData.Length} bytes long.");

string recoveredString = LoadCompressedFile(filename);
recoveredString = recoveredString.Substring(
0, recoveredString.Length / 100);
WriteLine($"\nRecovered data: {recoveredString}");

ReadKey();
}
catch (IOException ex)
{
WriteLine("An IO exception has been thrown!");
WriteLine(ex.ToString());
ReadKey();
}
}
}
}
36 changes: 36 additions & 0 deletions Chapter20/Compressor/Compressor/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Compressor")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Compressor")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("a3994be8-0b47-4522-9362-20b3861c2fd5")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
22 changes: 22 additions & 0 deletions Chapter20/FileWatch/FileWatch.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.15
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileWatch", "FileWatch\FileWatch.csproj", "{8996731A-8EEE-4B6F-AABA-1B76ECB47081}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8996731A-8EEE-4B6F-AABA-1B76ECB47081}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8996731A-8EEE-4B6F-AABA-1B76ECB47081}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8996731A-8EEE-4B6F-AABA-1B76ECB47081}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8996731A-8EEE-4B6F-AABA-1B76ECB47081}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions Chapter20/FileWatch/FileWatch/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1"/>
</startup>
</configuration>
9 changes: 9 additions & 0 deletions Chapter20/FileWatch/FileWatch/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="FileWatch.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FileWatch"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
17 changes: 17 additions & 0 deletions Chapter20/FileWatch/FileWatch/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace FileWatch
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
Loading

0 comments on commit 8d9d5b4

Please sign in to comment.