How to read and write files in C#?
In C#, reading and writing files is usually done using the File class in the System.IO namespace. Here is a simple example code demonstrating how to read and write files.
- Read the contents of the file.
string filePath = "path/to/your/file.txt";
string fileContent = File.ReadAllText(filePath);
Console.WriteLine(fileContent);
- Write the content into the file.
string filePath = "path/to/your/file.txt";
string content = "Hello, World!";
File.WriteAllText(filePath, content);
Please note that the path/to/your/file.txt in the example code above should be replaced with the path of the file you want to read from or write to. Additionally, you can also use the StreamWriter class to read and write file contents more flexibly.