Created
May 22, 2018 17:43
-
-
Save steveevers/beaf6fe57791c2f25d1f727bac79a501 to your computer and use it in GitHub Desktop.
Memory map and read a file. From here: https://stackoverflow.com/questions/45644934/notepad-beats-them-all
This file contains 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
using System.IO; | |
using System.IO.MemoryMappedFiles; | |
public static class FileUtils | |
{ | |
public static string MapAndRead(string processIdPath) { | |
using (var f = new FileStream(processIdPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) | |
using (var m = MemoryMappedFile.CreateFromFile(f, null, 0, MemoryMappedFileAccess.Read, null, HandleInheritability.None, true)) | |
using (var s = m.CreateViewStream(0, 0, MemoryMappedFileAccess.Read)) | |
using (var r = new StreamReader(s)) { | |
return r.ReadToEnd(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment