Я тестирую, как работают классы FileStream и StreamReader. Через консольное приложение. Я пытаюсь войти в файл и прочитать строки и распечатать их на консоли.
Я смог сделать это с помощью цикла while, но я хочу попробовать его с помощью цикла foreach.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace testing
{
public class Program
{
public static void Main(string[] args)
{
string file = @"C:\Temp\New Folder\New Text Document.txt";
using(FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
{
using(StreamReader sr = new StreamReader(fs))
{
foreach(string line in file)
{
Console.WriteLine(line);
}
}
}
}
}
}
Ошибка, которую я продолжаю получать для этого: Не могу преобразовать тип 'char' в 'string'
Цикл while, который работает, выглядит следующим образом:
while((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
Я, вероятно, пропущу что-то действительно основное, но я не вижу его.