C#:
Writing a text file
How to:
C# simplifies file operations with its System.IO
namespace, providing straightforward methods to write text files. Here’s how to write a basic text file and append text to an existing file.
Writing to a Text File from Scratch
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = @"C:\example\ExampleFile.txt";
string content = "Hello, world!";
// Write the content to a new file
File.WriteAllText(filePath, content);
Console.WriteLine("File written successfully.");
}
}
Sample Output:
File written successfully.
Appending Text to an Existing File
If you wish to add text to the end of an existing file, you can use File.AppendAllText
method.
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = @"C:\example\ExampleFile.txt";
string additionalContent = "\nAdding more content.";
// Append content to the file
File.AppendAllText(filePath, additionalContent);
Console.WriteLine("Content appended successfully.");
}
}
Sample Output:
Content appended successfully.
Using Third-Party Libraries: StreamWriter
For more fine-grained control over writing, including automatic flushing and encoding selection, use StreamWriter
.
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = @"C:\example\ExampleFile.txt";
string content = "This is an example using StreamWriter.";
// Using StreamWriter to write to a file
using (StreamWriter writer = new StreamWriter(filePath, append: true))
{
writer.WriteLine(content);
}
Console.WriteLine("File written with StreamWriter successfully.");
}
}
Sample Output:
File written with StreamWriter successfully.
Each of these approaches serves different needs: direct File
methods for quick operations, and StreamWriter
for more complex writing scenarios. Choose based on your specific requirements, considering factors like performance and file size.