meesie1 meesie1 - 6 months ago 30
C# Question

C# file reading

public IEnumerable<String> GetWords(string path)
{
if (File.Exists(path))
{
string[] lines = File.ReadAllLines(path);
foreach (string line in lines)
{

yield return line;
}
}
else
{
Console.WriteLine("Directory not correct");
}
}


Can someone help me solve this. I want to read a file but when I output it to the console, nothing is showing up.

Answer Source
public class Program
{
    public static void Main(string[] args)
    {
        foreach (string item in GetWords(@"C:\Temp\file.txt"))
        {
            Console.WriteLine("{0} ", item);
        }
    }

    public static IEnumerable<String> GetWords(string path) 
    {
        if (File.Exists(path))
        {
            foreach (var line in File.ReadAllLines(path))
            {
                yield return line;
            }
        }
        else
        {
            Console.WriteLine("Directory not correct");
            yield return null;
        }
    }
}