๐ŸŽ“ A1Learner
๐Ÿ”
0 of 38 lessons completed

C# Working with Lines

Some files hold one item on each line. The File class can write and read such a file as an array of lines.

Print the lines numbered

WriteAllLines stores three names. The loop prints each line with its position.

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string[] names = { "Ana", "Ben", "Cora" };
        File.WriteAllLines("names.txt", names);
        string[] lines = File.ReadAllLines("names.txt");
        for (int i = 0; i < lines.Length; i++)
        {
            Console.WriteLine((i + 1) + ". " + lines[i]);
        }
    }
}

Count the stored lines

The length of the read array is the number of lines. The last item sits at the final index.

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string[] colors = { "red", "green", "blue", "gold" };
        File.WriteAllLines("colors.txt", colors);
        string[] lines = File.ReadAllLines("colors.txt");
        Console.WriteLine("Lines: " + lines.Length);
        Console.WriteLine("Last: " + lines[lines.Length - 1]);
    }
}

Grow the file by one line

The lines move into a List so a new item can join. Writing the list back grows the file to four lines.

using System;
using System.Collections.Generic;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string[] start = { "milk", "bread", "eggs" };
        File.WriteAllLines("list.txt", start);
        List<string> items = new List<string>(File.ReadAllLines("list.txt"));
        items.Add("rice");
        File.WriteAllLines("list.txt", items);
        string[] lines = File.ReadAllLines("list.txt");
        Console.WriteLine("Lines: " + lines.Length);
        Console.WriteLine("Added: " + lines[3]);
    }
}

Example

using System;
using System.Collections.Generic;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string[] tasks = { "Wake up", "Eat breakfast", "Write code" };
        File.WriteAllLines("tasks.txt", tasks);
        List<string> list = new List<string>(File.ReadAllLines("tasks.txt"));
        list.Add("Sleep");
        File.WriteAllLines("tasks.txt", list);
        string[] lines = File.ReadAllLines("tasks.txt");
        for (int i = 0; i < lines.Length; i++)
        {
            Console.WriteLine((i + 1) + ". " + lines[i]);
        }
    }
}

Key points

  • using System.IO brings in the File class.
  • File.WriteAllLines writes each array item on its own line.
  • File.ReadAllLines reads a file into a string array.
  • Each array item holds one line of the file.
  • The line breaks are not part of the array items.
  • A List can hold the lines while a new line is added.
  • Writing the list back stores the longer file.

In this example, the program writes three task lines to a file. The lines move into a List and a fourth task is added. The list is written back, and the numbered printout shows all four lines.

Note

ReadAllLines keeps the order of the lines in the file.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does File.ReadAllLines return?