In the following example, we use the WriteAllText() method to create a file named "filename.txt" and write some content to it. Then we use the ReadAllText() method to read the contents of the file.
1. Using File.WriteAllText
This method writes a string to a text file. If the file does not exist, it creates a new one; if it exists, the content is overwritten.
using System.IO;
class Program
{
static void Main()
{
string filePath = "example.txt";
string content = "Hello, this is a sample text.";
File.WriteAllText(filePath, content);
Console.WriteLine("File written successfully!");
}
}
2. Using File.AppendAllText
This method appends a string to a text file. If the file does not exist, it creates a new one.
using System.IO;
class Program
{
static void Main()
{
string filePath = "example.txt";
string content = "Appending this line to the file.\n";
File.AppendAllText(filePath, content);
Console.WriteLine("Text appended successfully!");
}
}
3. Using StreamWriter
The StreamWriter class provides more control over file writing. It can write line-by-line and can also be used with using statements to automatically close the file.
using System.IO;
class Program
{
static void Main()
{
string filePath = "example.txt";
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.WriteLine("Line 1: Hello World!");
writer.WriteLine("Line 2: Writing to a file using StreamWriter.");
}
Console.WriteLine("File written using StreamWriter.");
}
}
In the following example, we use the WriteAllText() method to create a file named "filename.txt" and write some content to it. Then we use the ReadAllText() method to read the contents of the file.
1. Using File.WriteAllText
This method writes a string to a text file. If the file does not exist, it creates a new one; if it exists, the content is overwritten.
using System.IO; class Program { static void Main() { string filePath = "example.txt"; string content = "Hello, this is a sample text."; File.WriteAllText(filePath, content); Console.WriteLine("File written successfully!"); } }
2. Using File.AppendAllText
This method appends a string to a text file. If the file does not exist, it creates a new one.
using System.IO; class Program { static void Main() { string filePath = "example.txt"; string content = "Appending this line to the file.\n"; File.AppendAllText(filePath, content); Console.WriteLine("Text appended successfully!"); } }
3. Using StreamWriter
The StreamWriter class provides more control over file writing. It can write line-by-line and can also be used with using statements to automatically close the file.
using System.IO; class Program { static void Main() { string filePath = "example.txt"; using (StreamWriter writer = new StreamWriter(filePath)) { writer.WriteLine("Line 1: Hello World!"); writer.WriteLine("Line 2: Writing to a file using StreamWriter."); } Console.WriteLine("File written using StreamWriter."); } }
C# is primarily used on the Windows . NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved