• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

How to Read a File in C#: Quick and Efficient Methods

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Reading files is a fundamental skill in C#, and knowing the right method can improve both performance and readability. This article explores various ways to read text files, from the simplest to the most efficient, with practical code examples and guidance.

Table of Contents

  1. ReadAllText – The Quickest Way
  2. ReadAllLines – Easy Line-by-Line
  3. ReadLines – Best for Large Files
  4. StreamReader – Full Control
  5. Asynchronous Reading
  6. Benchmark Example
  7. Encoding Considerations
  8. File Existence Check
  9. Comparison Summary
  10. GitHub Example
  11. Frequently Asked Questions (FAQ)
1. ReadAllText – The Quickest Way


string content = File.ReadAllText("example.txt");
Console.WriteLine(content);


`

Best for small files when you need the entire content at once.

2. ReadAllLines – Easy Line-by-Line


csharp
string[] lines = File.ReadAllLines("example.txt");
foreach (var line in lines)
{
Console.WriteLine(line);
}

Ideal when processing individual lines but the file size is moderate.

3. ReadLines – Best for Large Files


csharp
foreach (var line in File.ReadLines("example.txt"))
{
Console.WriteLine(line);
}

This method uses lazy evaluation, making it memory-efficient for large files.

4. StreamReader – Full Control


csharp
using (var reader = new StreamReader("example.txt", Encoding.UTF8))
{
string? line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}

Use when you need to specify encoding or implement custom logic for each line.

5. Asynchronous Reading


Reading an entire file asynchronously:

csharp
string content = await File.ReadAllTextAsync("example.txt");
Console.WriteLine(content);

For asynchronous line-by-line reading:

`csharp
await foreach (var line in ReadLinesAsync("example.txt"))
{
Console.WriteLine(line);
}

async IAsyncEnumerable ReadLinesAsync(string path)
{
using var reader = new StreamReader(path);
while (await reader.ReadLineAsync() is { } line)
{
yield return line;
}
}
`

Recommended for GUI applications or web servers where blocking is undesirable.

6. Benchmark Example


csharp
var sw = Stopwatch.StartNew();
string content = File.ReadAllText("example.txt");
sw.Stop();
Console.WriteLine($"Elapsed time: {sw.ElapsedMilliseconds} ms");

Use similar code to compare different reading methods in your environment.

7. Encoding Considerations


To specify a particular encoding:

csharp
string content = File.ReadAllText("example.txt", Encoding.GetEncoding("Windows-1252"));

Useful encodings include:

  • Encoding.UTF8 (default)
  • Encoding.Unicode (UTF-16)
  • Encoding.ASCII
  • Encoding.GetEncoding("ISO-8859-1")
8. File Existence Check


Always check if the file exists to avoid exceptions:

csharp
if (!File.Exists("example.txt"))
{
Console.WriteLine("File not found.");
return;
}

9. Comparison Summary

MethodBest ForMemory UseAsyncLazy
ReadAllTextSmall filesHighYesNo
ReadAllLinesSmall/medium filesHighYesNo
ReadLinesLarge filesLowNoYes
StreamReaderFull controlMediumYesYes
ReadAllTextAsyncAsync + small filesHighYesNo
StreamReader + asyncAsync + large filesLowYesYes
10. GitHub Example


The full example project will be available at:


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



11. Frequently Asked Questions (FAQ)

What is considered a large file to read into memory?


As a general rule:

  • Under 10 MB: Safe for full read
  • 10–100 MB: Use with caution
  • Over 100 MB: Use streaming (ReadLines or StreamReader)
How to read a CSV file in C#?


For simple CSV parsing:

csharp
foreach (var line in File.ReadLines("data.csv"))
{
var fields = line.Split(',');
Console.WriteLine($"Name: {fields[0]}, Age: {fields[1]}");
}

For complex CSV files, use a library like

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

.

How to check if a file exists before reading it in C#?


Use File.Exists:

csharp
if (!File.Exists("example.txt"))
{
Console.WriteLine("The file does not exist.");
return;
}


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу