23

I have see this term branded around but I don't really understand how you open a file in memory.

I have the files written to disk in a temp location but this needs cleaning when a certain form closes and I can't do it when it's open. It's a must that this folder gets emptied. I was wondering if I opened files in memory instead whether it would make a difference?

Null
  • 1,950
  • 9
  • 30
  • 33
rik
  • 1,279
  • 4
  • 20
  • 29

4 Answers4

60
MemoryStream inMemoryCopy = new MemoryStream();
using (FileStream fs = File.OpenRead(path))
{
  fs.CopyTo(inMemoryCopy);
}
// Now you can delete the file at 'path' and still have an in memory copy
Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
  • dam it, we are using .net 3.5 and as such i cant use fs.copyto – rik Jun 03 '11 at 13:10
  • you can easily replicate that CopyTo extension method yourself. Search here. – Teoman Soygul Jun 03 '11 at 13:13
  • you mean search stack over flow for copy to extension method? – rik Jun 03 '11 at 13:24
  • yes there were some very good suggestions mimicking the original CopyTo method in .net 4 – Teoman Soygul Jun 03 '11 at 13:34
  • http://stackoverflow.com/questions/230128/best-way-to-copy-between-two-stream-instances-c the filestream becomes input, the inmemory stream becomes output right? how do i then display that file? sorry to be a pain i am just massively confused! – rik Jun 03 '11 at 13:55
  • 1
    true. Open(..) sort of methods usually accept either file path or stream as input. Use the methods accepting stream as the parameter then you're good to go. – Teoman Soygul Jun 03 '11 at 14:04
  • on what type of object are we talking here - an adobe object? – rik Jun 03 '11 at 14:10
5

I think you want to work with Memory Mapped files added recently to .NET 4.

http://blogs.msdn.com/b/salvapatuel/archive/2009/06/08/working-with-memory-mapped-files-in-net-4.aspx

Memory Mapped Files .NET

Community
  • 1
  • 1
kenny
  • 21,522
  • 8
  • 49
  • 87
  • 2
    Good thinking but memory mapped files still retain their mapping to the original file and write back any in-memory changes to the file after un-mapping, so not that useful in this scenario. – Teoman Soygul Jun 03 '11 at 13:07
4

You can use DeleteOnClose parameter of FileStream constructor:

FileStream fs = new FileStream("<Path Here>", FileMode.Create,
    FileAccess.ReadWrite, FileShare.None, 1024, FileOptions.DeleteOnClose);

and the file will be deleted when closed.

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Rajeev
  • 4,571
  • 2
  • 22
  • 35
3

I think it means to read the content of that file into memory as a whole and then close the connection to the file. Assuming it's a file that's not too big you could just read it into a byte[]:

byte[] fileContent = File.ReadAllBytes(fileName);

If it's a text file read it into a string using

string fileContent = File.ReadAllText(fileName);

Once you've done that use a StreamReader to read it later as you would a file on disk.

Peter Wiles
  • 396
  • 2
  • 4