JavaNoob
JavaNoob

Reputation: 3632

C# how to convert File.ReadLines into string array?

The question that I have is regarding converting the process of reading lines from a text file into an array instead of just reading it.

The error in my codes appear at string[] lines = File.ReadLines("c:\\file.txt"); with cannot implicitly convert....

Can someone please advise on the codes to save the results in an array format? I've placed the ReadAllLines code which is able to save the results in an array too. Thanks!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace Testing
{
class Analysis
{
    static void Main()
    {
        string[] lines = File.ReadLines("c:\\file.txt");

        foreach (string r in lines)
        {
            Console.WriteLine("-- {0}", r);
        }

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
}

ReadAllLines Codes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace Testing
{
class ReadFromFile
{
    static void Main()
    {
        string[] lines = System.IO.File.ReadAllLines
        (@"C:\Users\Public\TestFolder\WriteLines2.txt");

        System.Console.WriteLine("Contents of writeLines2.txt =:");
        foreach (string line in lines)
        {
            Console.WriteLine("\t" + line);
        }

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
}

Upvotes: 34

Views: 158214

Answers (4)

Jim Mischel
Jim Mischel

Reputation: 134115

string[] lines = File.ReadLines("c:\\file.txt").ToArray();

Although one wonders why you'll want to do that when ReadAllLines works just fine.

Or perhaps you just want to enumerate with the return value of File.ReadLines:

var lines = File.ReadLines("c:\\file.txt");
foreach (var line in lines)
{
    Console.WriteLine("\t" + line);
}

Upvotes: 27

Gerald Davis
Gerald Davis

Reputation: 4549

File.ReadLines() returns an object of type System.Collections.Generic.IEnumerable<String>
File.ReadAllLines() returns an array of strings.

If you want to use an array of strings you need to call the correct function.

You could use Jim solution, just use ReadAllLines() or you could change your return type.

This would also work:

System.Collections.Generic.IEnumerable<String> lines = File.ReadLines("c:\\file.txt");

You can use any generic collection which implements IEnumerable, such as IList<String>.

Upvotes: 56

Mohammad Bourm
Mohammad Bourm

Reputation: 1

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace FileReader
{
    class Program
    {
        static void Main(string[] args)
        {
            var lines = File.ReadAllLines("D:/Text.txt").ToList();
            if(lines != null && lines.Count  > 0)
            {
                foreach(var line in lines)
                {
                    Console.WriteLine(line);
                }
            }
            Console.ReadKey();
        }
    }
}

Upvotes: -2

Greg Sansom
Greg Sansom

Reputation: 20860

Change string[] lines = File.ReadLines("c:\\file.txt"); to IEnumerable<string> lines = File.ReadLines("c:\\file.txt"); The rest of your code should work fine.

Upvotes: 2

Related Questions