PeNet provides several ways to open and parse a PE file. The best method depends on your requirements for memory usage, performance, and whether you intend to modify the file.
1. Byte Array (Small Files / Modification)
Best for very small files or when you want to write/change the PE file without affecting the original on disk. A full copy of the file is held in memory; changes are made to the copy, which you can then save to a new file.
- Warning: Memory usage scales with file size. Performance decreases for files larger than 10 MB.
2. Stream (Low Memory / Constant Usage)
Best for keeping memory usage low and constant regardless of file size.
- Warning: Performance is lower than other methods. If you modify the stream, changes are written directly to the original file, which may cause unwanted side-effects.
3. Memory Mapped File (Large Files / High Performance)
The fastest method for large files with the lowest memory consumption.
- Warning: Like streams, all writes are applied directly to the original input file.
// Byte Array
var bin = File.ReadAllBytes(@"C:\Windows\System32\kernel32.dll");
var peHeader = new PeNet.PeFile(bin);
// Stream
using var fileStream = File.OpenRead(@"C:\Windows\System32\kernel32.dll");
var peHeader = new PeNet.PeFile(fileStream);
// Memory Mapped File
using var mmf = new PeNet.FileParser.MMFile(@"C:\Windows\System32\kernel32.dll");
var peHeader = new PeNet.PeFile(mmf);