| |
With any programming language there is always a need to read and write to files. In C#, this is done quite easily compared to many other programming languages.
Like other languages, C# utilizes a Stream class, which gives basic functionality when working with streams (any situation where you need to read and write data to some endpoint). The class FileStream, which is actually derived from the Stream class, adds to Stream the functionaly needed to read and write to files.
Now, let's get started!
The first thing you need to do is add the correct library at the top:
This provides us with the functionality we will need for this tutorial.
Now, create an instance of FileStream:
FileStream file = new FileStream("path.txt", FileMode, FileAccess);
|
There are several overloads for this constructor, but this is the easiest to use for this tutorial. FileMode specifies what the FileStream should do with the file to begin with. The options are: Append, Create, CreateNew, Open, OpenOrCreate, or Truncate, and can be accessed by FileMode.option.
FileAccess specifies the file priveledges. The options are: Read, ReadWrite, or Write, and can be accesed by FileAccess.option.
Writing to a file:
To write to a file we will need to use class StreamWriter, which is derived from the TextWriter class.
StreamWriter sw = new StreamWriter(file);
|
The constructor for StreamWriter also has several overloads. The one showed here takes an instance of FileStream, instantiated above (file).
With StreamWriter, you have two options for writing to a file: Write, or WriteLine. The Write method has 21 overloads, and the WriteLine method has 18, so you should have no problem finding the correct functionality to suit your programming needs.
Here is a simple example:
sw.Write("Hello file system world!");
|
Now don't forget to close the stream when you are done!
Reading from a file:
To read from a file we will need to use class StreamReader. Similar to StreamWriter, StreamReader is derived from the TextReader class.
StreamReader sr = new StreamReader(file);
|
Just like StreamWriter there are several posibilities for constructor overloads. This one takes an instance of FileStream.
With StreamReader, there are four options for reading from a file: Read, ReadBlock, ReadLine, or ReadToEnd. Each one of these functions has several overloads. For the purposes of this example, I will use ReadToEnd, which just like the title implies, reads the entire file.
string s = sr.ReadToEnd();
|
Again, don't forget to close the stream!
And when you are all done with the file, make sure to close that too.
Thats it! Thats all there is to it! Pretty simple if you ask me.
For those of you who learn better by visually looking at code samples, here is the entire thing one more time:
// *** Write to file ***
// Specify file, instructions, and privelegdes
FileStream file = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Write);
// Create a new stream to write to the file
StreamWriter sw = new StreamWriter(file);
// Write a string to the file
sw.Write("Hello file system world!");
// Close StreamWriter
sw.Close();
// Close file
file.Close();
// *** Read from file ***
// Specify file, instructions, and privelegdes
file = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Read);
// Create a new stream to read from a file
StreamReader sr = new StreamReader(file);
// Read contents of file into a string
string s = sr.ReadToEnd();
// Close StreamReader
sr.Close();
// Close file
file.Close();
|
This is by no means the only way to read and write to files. FileStream, StreamWriter, and StreamReader each provide many more advanced features.
In my next tutorial I will discuss more advanced file features, as well as show you how to use file dialogs in C#!
Good Luck!
|
|