File writing basics
There cannot be anything more straightforward than writing to a file, right? That’s why I think that that is a good starting point. Here is the code to do so:
var path = System.IO.Path.GetTempPath(); var fileName = "WriteLines.txt"; var fullPath = Path.Combine(path, fileName); File.WriteAllText(fullPath, "Hello, System Programmers");
The first line gets the system temp
path. Then we specify the filename, add that to the temp
path, and write a line of text to that file.
This example is simple enough, but it already shows something useful. First, we can get to the temp
folder quickly; we don’t have to specify where that is in our code. Second, we can combine the filename and the path without worrying about the path separator. On Windows, the parts of the path are separated by a backslash, while on Linux, this is a forward slash. The CLR figures out what it should use and uses the correct one.
The File.WriteAllText
then...