mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-04-06 06:43:47 +00:00
Initial commit
This commit is contained in:
parent
17ad36d6ad
commit
3d241b2154
44 changed files with 1619 additions and 0 deletions
94
vs4win/Chapter09/WorkingWithStreams/Program.cs
Normal file
94
vs4win/Chapter09/WorkingWithStreams/Program.cs
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
using System.Xml;
|
||||
|
||||
using static System.Environment;
|
||||
using static System.IO.Path;
|
||||
|
||||
SectionTitle("Writing to text streams");
|
||||
|
||||
// define a file to write to
|
||||
string textFile = Combine(CurrentDirectory, "streams.txt");
|
||||
|
||||
// create a text file and return a helper writer
|
||||
StreamWriter text = File.CreateText(textFile);
|
||||
|
||||
// enumerate the strings, writing each one
|
||||
// to the stream on a separate line
|
||||
foreach (string item in Viper.Callsigns)
|
||||
{
|
||||
text.WriteLine(item);
|
||||
}
|
||||
text.Close(); // release resources
|
||||
|
||||
// output the contents of the file
|
||||
WriteLine("{0} contains {1:N0} bytes.",
|
||||
arg0: textFile,
|
||||
arg1: new FileInfo(textFile).Length);
|
||||
|
||||
WriteLine(File.ReadAllText(textFile));
|
||||
|
||||
SectionTitle("Writing to XML streams");
|
||||
|
||||
// define a file path to write to
|
||||
string xmlFile = Combine(CurrentDirectory, "streams.xml");
|
||||
|
||||
// declare variables for the filestream and XML writer
|
||||
FileStream? xmlFileStream = null;
|
||||
XmlWriter? xml = null;
|
||||
|
||||
try
|
||||
{
|
||||
// create a file stream
|
||||
xmlFileStream = File.Create(xmlFile);
|
||||
|
||||
// wrap the file stream in an XML writer helper
|
||||
// and automatically indent nested elements
|
||||
xml = XmlWriter.Create(xmlFileStream,
|
||||
new XmlWriterSettings { Indent = true });
|
||||
|
||||
// write the XML declaration
|
||||
xml.WriteStartDocument();
|
||||
|
||||
// write a root element
|
||||
xml.WriteStartElement("callsigns");
|
||||
|
||||
// enumerate the strings writing each one to the stream
|
||||
foreach (string item in Viper.Callsigns)
|
||||
{
|
||||
xml.WriteElementString("callsign", item);
|
||||
}
|
||||
|
||||
// write the close root element
|
||||
xml.WriteEndElement();
|
||||
|
||||
// close helper and stream
|
||||
xml.Close();
|
||||
xmlFileStream.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// if the path doesn't exist the exception will be caught
|
||||
WriteLine($"{ex.GetType()} says {ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (xml != null)
|
||||
{
|
||||
xml.Dispose();
|
||||
WriteLine("The XML writer's unmanaged resources have been disposed.");
|
||||
if (xmlFileStream != null)
|
||||
{
|
||||
xmlFileStream.Dispose();
|
||||
WriteLine("The file stream's unmanaged resources have been disposed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// output all the contents of the file
|
||||
WriteLine("{0} contains {1:N0} bytes.",
|
||||
arg0: xmlFile,
|
||||
arg1: new FileInfo(xmlFile).Length);
|
||||
WriteLine(File.ReadAllText(xmlFile));
|
||||
|
||||
SectionTitle("Compressing streams");
|
||||
Compress(algorithm: "gzip");
|
||||
Compress(algorithm: "brotli");
|
||||
Loading…
Add table
Add a link
Reference in a new issue