Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a "directory" in memory?

I'm working in c#, and looking for a way to create a path to a directory that will map to an IO.Stream instead of to the actual file system.
I want to be able to "save" files to that path, manipulate the content or file names, and then save them from that path to a regular file in the file system.

I know I can use a temporary file, but I would rather use the memory for both security and performance.

This kind of thing exists, according to this answer, in Java, using the FileSystemProvider class. I'm looking for a way to do it in c#.

I've tried every search I could think of and came up only with the java answer and suggestions to use Temporary files.
Is it even possible using .net?

Basically, I'm looking for a way to enable saving files directly to memory as if they where saved into the file system. so, for instance, if I had a 3rd party class that exposes a save method (save(string fullPath)), or something like the SmtpServer.Send(MyMsg) in this question, i could choose that path and save it into the memory stream instead of onto the drive. (the main thing here is that I want to provide a path that will lead directly to a memory stream).

like image 567
Zohar Peled Avatar asked May 10 '15 09:05

Zohar Peled


1 Answers

.NET doesn't have an abstraction layer over the host OS's file system. So unless you can build your own for use in custom code, and you need to have 3rd party libraries covered, there are just two workable optilns:

  • Use streams and avoid any APIs working with file names.
  • Build a virtual file system plugged into your host OS's storage architecture; however, the effort needed versus benefits is highly questionable.
like image 200
Ondrej Tucny Avatar answered Sep 20 '22 00:09

Ondrej Tucny