Skip to content

Instantly share code, notes, and snippets.

@wocar
Last active September 22, 2020 06:36
Show Gist options
  • Select an option

  • Save wocar/48a23e6cd23b7e17a36f42123583c9cf to your computer and use it in GitHub Desktop.

Select an option

Save wocar/48a23e6cd23b7e17a36f42123583c9cf to your computer and use it in GitHub Desktop.
Helper to modify a file line by line without loading it into to memory
async Task Transform(Func<string, string> transform)
{
var tmp = Path.GetTempFileName();
await using var writer = new StreamWriter(tmp, false);
using var reader = new StreamReader(FileName);
var line = await reader.ReadLineAsync();
while (line != null)
{
var newLine = transform(line);
await writer.WriteLineAsync(newLine);
line = await reader.ReadLineAsync();
}
File.Copy(tmp, FileName, true);
File.Delete(tmp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment