In this tutorial, we’ll take a look at how to work with files — both creating and modifying them.
Index
1 - Absolute and Relative Paths
The first thing we’ll look at is absolute and relative paths, since these are what we’ll use to access the files themselves.
The difference between the two is quite simple:
- With relative paths, we access files depending on the folder or directory we’re currently in.
- On the other hand, absolute paths are based from the root directory of the computer, usually
C:\
on Windows or/
on Linux.
Here’s a visual example of both absolute and relative paths:
As shown on the left, we’re initially located in the C:\App1
directory, so to move up a submenu we use ..\
, while for absolute paths it doesn’t matter where we are — since we always start by indicating C:\
.
In the professional world, since applications may be located on different servers, absolute paths are used to locate files.
2 - Reading a File
2.1 - StreamReader Type
To read an existing file, we use the StreamReader()
type and put it inside a using
statement.
Why do we instantiate it inside a using
statement?
Because when the code block created by using
closes, it automatically destroys from memory all the variables initialized within it. This isn’t a problem for a very small application, but when we have a big app, or one that needs to read several gigabytes of information, it can become a big problem and cause the system to fail.
StreamReader
has a default method called .ReadLine()
which reads each line of the document, allowing us to work with it.
//Access the fileusing (StreamReader reader = new StreamReader("c:\ejemplo\fichero.txt")) { string text; //Create the variable that will hold the text while ((text = reader.ReadLine()) != null) //Read line by line { Console.WriteLine(text); //Show the information }}
2.2 - File Type
Another option is the File
class, which reads the file all at once. As you’ll see throughout my posts, I care quite a bit about memory — so if the file is very large, this may not be the best idea.
string filereader = File.ReadAllText("c:\ejemplo\fichero.txt");
3 - Writing a File
3.1 - StreamWriter Type
Writing is also very straightforward, in this case we use the StreamWriter
type, and we must provide the text or list of text to write.
string[] paises = new string[] { "USA", "Inglaterra", "Alemaia" };using (StreamWriter writer = new StreamWriter("c:\ejemplo\fichero.txt")){ foreach (string item in countries) { writer.WriteLine(item); }}
This method has a small downside — if we want to add another line of text later, what happens behind the scenes is that the entire file is rewritten. Furthermore, if the file doesn’t exist, it will be created for us.
3.2 - File Type
As in the previous case, we can use the File
type to write. In this case, we have several options:
string[] paises = new string[] { "USA", "England", "Germany" }; File.WriteAllText("c:\ejemplo\fichero.txt", "China Japon Korea");File.WriteAllLines("c:\ejemplo\fichero.txt", paises);
File.WriteAllText()
will write all the text in one go.File.WriteAllLines()
will write each element of thearray
as a separate line.
If there is any problem you can add a comment bellow or contact me in the website's contact form