Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Writing Files | Additional Structures & File Handling
C# Beyond Basics

book
Writing Files

For writing text in a file we create a StreamWriter object with the path of the file, similar to how we create a StreamReader object.

Note

The platform currently doesn't support file writing so this can be practiced on a compiler on your PC.

The StreamWriter object offers a WriteLine method which can write text into a file. We will now try to write something to the text.txt file.

cs

index

copy
var textFile = new StreamWriter("C:/Users/Admin/Desktop/text.txt");
textFile.WriteLine("This is some new text.");
textFile.WriteLine("This is the next line.");
textFile.WriteLine("Another line.");
textFile.Close();
12345
var textFile = new StreamWriter("C:/Users/Admin/Desktop/text.txt"); textFile.WriteLine("This is some new text."); textFile.WriteLine("This is the next line."); textFile.WriteLine("Another line."); textFile.Close();

The above code will overwrite the previous content of the text.txt file and replace it with:

This is some new text.
This is the next line.
Another line.

StreamWriter has a second parameter called "append" which takes in a boolean value and is set to false by default. If we set the append parameter to true then the code will not overwrite the older text rather it would append the new text to the end of the file.

For-example, consider the file text.txt has the following text in it:

txt

text

copy
First line
Second line
Third line
123
First line Second line Third line

We will use the following code to add a new line to it:

cs

index

copy
using System;
using System.IO;

public class HelloWorld
{
public static void Main(string[] args)
{
var textFile = new StreamWriter("C:/Users/Admin/Desktop/text.txt", true);
textFile.WriteLine("This is a new line.");
textFile.Close();
}
}
123456789101112
using System; using System.IO; public class HelloWorld { public static void Main(string[] args) { var textFile = new StreamWriter("C:/Users/Admin/Desktop/text.txt", true); textFile.WriteLine("This is a new line."); textFile.Close(); } }

The file will look like this after running the code:

txt

text

copy
First line
Second line
Third line
This is a new line.
1234
First line Second line Third line This is a new line.

1. What is the correct way of creating a StreamWriter object for appending text into a file?

2. What method is used to write a line to a text file using the StreamWriter class?

3. Which method is used for closing a file after use?

question mark

What is the correct way of creating a StreamWriter object for appending text into a file?

Selecione a resposta correta

question mark

What method is used to write a line to a text file using the StreamWriter class?

Selecione a resposta correta

question mark

Which method is used for closing a file after use?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 8
some-alt