StreamReader
class to read a text file. The Write a text file (example 1) and the Write a text file (example 2) sections describe how to use the StreamWriter
class to write text to a file.StreamReader
class to open, to read, and to close the text file. You can pass the path of a text file to the StreamReader
constructor to open the file automatically. The ReadLine
method reads each line of text, and increments the file pointer to the next line as it reads. When the ReadLine
method reaches the end of the file, it returns a null reference. For more information, see StreamReader Class.Main
method:StreamWriter
class to open, to write, and to close the text file. In a similar way to the StreamReader
class, you can pass the path of a text file to the StreamWriter
constructor to open the file automatically. The WriteLine
method writes a complete line of text to the text file.Main
method:StreamWriter
class to open, to write, and to close the text file. Unlike the previous example, this code passes two additional parameters to the constructor. The first parameter is the file path and the file name of the file. The second parameter, true
, specifies that the file is opened in append mode. If you specify false
for the second parameter, the contents of the file are overwritten each time you run the code. The third parameter specifies Unicode
, so that StreamWriter
encodes the file in Unicode format. You can also specify the following encoding methods for the third parameter:Write
method is similar to the WriteLine
method, except that the Write
method doesn't automatically embed a carriage return or line feed (CR/LF) character combination. It's useful when you want to write one character at a time.Main
method:try-catch-finally
block to handle errors and exceptions. Specifically, you may want to release handles to the file in the final block so that the file isn't locked indefinitely. Mac desktop remote control app. Mobius action camera mac software. Some possible errors include a file that doesn't exist, or a file that is already in use.Program
, with a single method, Main
, that takes a String array as an argument:Main
is the application entry point, the method that's called automatically by the runtime when it launches the application. Any command-line arguments supplied when the application is launched are available in the args
array.Main
method, which is the line that calls Console.WriteLine
, with the following code:name
. It also retrieves the value of the DateTime.Now property, which contains the current local time, and assigns it to a variable named date
. Finally, it displays these values in the console window.n
represents a newline character.$
) in front of a string lets you put expressions such as variable names in curly braces in the string. The expression value is inserted into the string in place of the expression. This syntax is referred to as interpolated strings.