0

I am using a library to create a PDF file from HTML, then I print that file and delete it from disk. The issue is, we don't want anything stored on disk, not even temporarily. I thought about using some kind of RamDisk, but I see MemoryMappedFile thrown around everywhere. I looked into it, but I don't think that provides the functionality I want since the file should already exist on disk before using it in a MemoryMappedFile.

My question is: Is my assumption true about the file needing to be on disk FIRST before using it in a MemoryMappedFile? IS there a way to create a MemoryMappedFile and access it using a virtual path without it existing on disk?

In case MemoryMappedFile doesn't work, what library can I use to create a virtual RamDisk with C#? It will only be storing 1 file at a time.

Majd Odeh
  • 181
  • 1
  • 11
  • It doesn't require a file, use CreateNew() instead of CreateFromFile(). The backing store is then provided by the paging file. Which still uses the disk, but you can't easily see that. Biggest problem with an MMF is that you need to pick a size, not easy to do for a pdf. A very simple other way to use paging file storage is by using MemoryStream. – Hans Passant Dec 12 '22 at 15:43
  • If your libraries has an API that uses `Stream` instead of file-names you can just use a MemoryStream, that will likely be by far the easiest solution. And I would argue that well designed APIs *should* accept streams, so if this is not possible you might want to look for some other library. – JonasH Dec 12 '22 at 15:59
  • The library I use to convert HTML to PDF does not take a strem unfortunately, but it takes the file path it should convert the PDF to. I will look into the CreateNew you mentioned but it seems the only option here is a RamDisk – Majd Odeh Dec 13 '22 at 05:10

0 Answers0