How to write file using StreamWriter in C#?
How to write file using StreamWriter in C#?
556
13-Jun-2024
Updated on 13-Jun-2024
Ashutosh Kumar Verma
13-Jun-2024C# StreamWriter for writing file
In c#, you can write the text to a file using the StreamWriter class.
Example-
Here is a simple example to demonstrates the StreamWriter in c# for writing the text to the file,
In the above example-
pathwhere we want to write the text (sample.txt).StreamWriterinstance named writer and pass the file path to its constructor.WriteLinemethod of theStreamWriterinstance to write text to the file.StreamWriteris properly disposed of after use, which locks the file and frees any associated objects.Output-
Also, Read: Difference between Array and ArrayList