diff options
Diffstat (limited to 'ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites')
4 files changed, 124 insertions, 0 deletions
diff --git a/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/index.md b/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/index.md new file mode 100644 index 0000000..8979ecc --- /dev/null +++ b/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/index.md @@ -0,0 +1,9 @@ +# Prerequisites + +Here are some prerequisites that are needed for using CsvHelper. These are .NET basics that are implied knowledge when using CsvHelper. [Microsoft has excellent documentation](https://docs.microsoft.com/dotnet/){target=_blank} that can you can use to learn more. + +Topics | +- | - +[Using and Dispose](~/examples/prerequisites/using-and-dispose) | +[Reading and Writing Files](~/examples/prerequisites/reading-and-writing-files) | +[Streams](~/examples/prerequisites/streams) | diff --git a/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/reading-and-writing-files/index.md b/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/reading-and-writing-files/index.md new file mode 100644 index 0000000..c5f4f8f --- /dev/null +++ b/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/reading-and-writing-files/index.md @@ -0,0 +1,65 @@ +# Reading and Writing Files + +To open a file for reading or writing, we can use `System.IO.File`. + +```cs +using (var stream = File.OpenRead("path\\to\\file.csv")) +{ +} + +using (var stream = File.OpenWrite("path\\to\\file.csv")) +{ +} +``` + +These both return a `FileStream` for working with our file. Since our data is text, we will need to use a `StreamReader` and `StreamWriter` to read and write the text. + +```cs +using (var stream = File.OpenRead("path\\to\\file.csv")) +using (var reader = new StreamReader(stream)) +{ +} + +using (var stream = File.OpenWrite("path\\to\\file.csv")) +using (var writer = new StreamWriter(stream)) +{ +} +``` + +`StreamReader` and `StreamWriter` have shortcuts for doing this. + +```cs +using (var reader = new StreamReader("path\\to\\file.csv")) +{ +} + +using (var writer = new StreamWriter("path\\to\\file.csv")) +{ +} +``` + +CsvHelper doesn't know anything about your encoding, so if you have a specific encoding, you'll need to specify that in your stream. + +```cs +using (var reader = new StreamReader("path\\to\\file.csv", Encoding.UTF8)) +{ +} + +using (var writer = new StreamWriter("path\\to\\file.csv", Encoding.UTF8)) +{ +} +``` + +`CsvReader` and `CsvWriter` take a `TextReader` and `TextWriter` in their constructors. `TextReader` and `TextWriter` are `abstract` classes for reading and writing text. `StreamReader` inherits `TextReader` and `StreamWriter` inherits `TextWriter`, so we can use those with `CsvReader` and `CsvWriter`. + +```cs +using (var reader = new StreamReader("path\\to\\file.csv")) +using (var csv = new CsvReader(reader)) +{ +} + +using (var writer = new StreamWriter("path\\to\\file.csv")) +using (var csv = new CsvWriter(writer)) +{ +} +``` diff --git a/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/streams/index.md b/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/streams/index.md new file mode 100644 index 0000000..a470129 --- /dev/null +++ b/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/streams/index.md @@ -0,0 +1,29 @@ +# Streams + +When reading from a stream, if you need to go back to the beginning of the stream, you can use the `Stream.Position` property. + +```cs +using (var stream = new File.OpenRead("path\\to\\file")) +using (var reader = new StreamReader(stream)) +{ + // Read file content. + var content = reader.ReadToEnd(); + + // Go back to beginning of the stream. + stream.Position = 0; + + // Read file content again. + content = reader.ReadToEnd(); +} +``` + +When writing to a file, you need to flush the writer for the data to be written to the stream. `StreamWriter` contains an internal buffer and the data is only written to the stream when the buffer is full, or `Flush` is called. `Flush` is automatically called when a `using` block exits. + +```cs +using (var stream = new File.OpenWrite("path\\to\\file")) +using (var writer = new StreamWriter(stream)) +{ + writer.WriteLine("Foo"); + writer.Flush(); // Data is written from the writer buffer to the stream. +} // Flush is also called here. +``` diff --git a/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/using-and-dispose/index.md b/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/using-and-dispose/index.md new file mode 100644 index 0000000..468d381 --- /dev/null +++ b/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/using-and-dispose/index.md @@ -0,0 +1,21 @@ +# Using and Dispose + +Whenever you have an object the implements `IDisposable`, you need to dispose of the resource when you're done with it. Most classes that use unmanaged resources will implement `IDisposable`. This means a lot of classes in the `System.IO` namespace will need to be disposed of. + +The best practice to dispose of an object when you're done with it is to wrap the code in a `using` block. When the `using` block exits, the resource will automatically be disposed of as soon as possible. + +```cs +using (var stream = new MemoryStream()) +{ + // Use the stream. +} +// The stream will be disposed of as soon as possible. +``` + +If you need to keep keep it around for a while and dispose of it later, `using` does some error handling for you, so it's still a good idea to use it instead of calling `Dispose` directly. There is some debate on whether this is a good idea because it doesn't show intent. + +```cs +var stream = new MemoryStream(); +// Later in a different part of your code. +using (stream) { } +``` |