In my current project, I wanted to ‘fake out’ the filesystem for integration tests. In several places in my code, I would first write some data to a temporary file, then later, open the file again for background processing. Now my integration tests run fine using the real filesystem, but just to make sure my tests could run in parallel, I wanted to switch to a faked filesystem.
So, my first attempt used memory streams. But then I found a crucial difference between filestreams and memorystreams. If you dispose a filestream.. the actual file is still there! Not so much with a memorystream.
So here’s an in memory filestream. If you close / dispose it.. it will still be there. Note.. it will only be cleaned up by the garbage collector. So don’t hang on to them in definetly and don’t use this in production.