Last active
September 22, 2020 06:36
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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