Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to write an array to a file
Writing an array to a file in C# can be accomplished using the File.WriteAllLines method from the System.IO namespace. This method writes each array element as a separate line to the specified file.
Syntax
Following is the syntax for using File.WriteAllLines −
File.WriteAllLines(string path, string[] contents);
Parameters
-
path − The file path where the array will be written
-
contents − The string array to write to the file
Using WriteAllLines Method
The WriteAllLines method automatically creates the file if it doesn't exist and overwrites it if it does. Each array element is written as a separate line in the file −
using System.IO;
using System;
public class Program {
public static void Main() {
string[] stringArray = new string[] {
"one",
"two",
"three"
};
File.WriteAllLines("new.txt", stringArray);
using (StreamReader sr = new StreamReader("new.txt")) {
string res = sr.ReadToEnd();
Console.WriteLine(res);
}
}
}
The output of the above code is −
one two three
Writing Different Array Types
For non-string arrays, you need to convert elements to strings first −
using System.IO;
using System;
using System.Linq;
public class Program {
public static void Main() {
int[] numbers = { 10, 20, 30, 40, 50 };
// Convert int array to string array
string[] stringNumbers = numbers.Select(x => x.ToString()).ToArray();
File.WriteAllLines("numbers.txt", stringNumbers);
// Read and display the file content
string[] lines = File.ReadAllLines("numbers.txt");
Console.WriteLine("File contents:");
foreach (string line in lines) {
Console.WriteLine(line);
}
}
}
The output of the above code is −
File contents: 10 20 30 40 50
Using WriteAllLines with File Path
You can specify a complete file path including directory −
using System.IO;
using System;
public class Program {
public static void Main() {
string[] fruits = { "Apple", "Banana", "Orange", "Mango" };
string filePath = Path.Combine(Environment.CurrentDirectory, "fruits.txt");
File.WriteAllLines(filePath, fruits);
Console.WriteLine("Array written to: " + filePath);
// Verify the file was created
if (File.Exists(filePath)) {
string[] readLines = File.ReadAllLines(filePath);
Console.WriteLine("Total lines written: " + readLines.Length);
}
}
}
The output of the above code is −
Array written to: C:\path\to\current\directory\fruits.txt Total lines written: 4
Comparison of File Writing Methods
| Method | Purpose | Usage |
|---|---|---|
| WriteAllLines | Write array elements as separate lines | Best for arrays where each element is a line |
| WriteAllText | Write entire string to file | Best for single string content |
| AppendAllLines | Append array elements to existing file | Best when you don't want to overwrite |
Conclusion
The File.WriteAllLines method provides a simple and efficient way to write array contents to a file in C#. Each array element is automatically written as a separate line, making it ideal for storing lists of data that can be easily read back later.
