0

I have an application that in a certain moment has to create a temporary file and then delete it. So I'm thinking about whether it is possible to create a temporary file into memory instead of directly into disk. Then I would like to read it from memory to perform some actions and finally after doing some stuff, delete if from memory. If it is possible, how to do this in .NET Framework and C#. Also, Can performance be affected by using directly memory and not disk?

I would like to use memory directly instead of disk because I am afraid of some kind of windows permissions that does not allow to write file to some places of disk... so another doubt is: if using disk instead of memory is there a safe place to write temporary files and avoid writting permissions conflicts for windows XP,Vista,7,8? I mean, a place where is always safe to write temporary files and then delete it. Maybe, in user\Local Settings\Temp folder? is it the best place? I would like to know a safe place to save temporary files and then delete them that is working for windows versions: XP, Vista, 7, and 8.

Thanks.

Willy
  • 9,848
  • 22
  • 141
  • 284

2 Answers2

2

You can use the MemoryStream for writing to memory instead of disk. Preformance can possibly be better using a memorystream because disks are often slower to write to. As always: profile before optimizing.

For information about the temporary folder see this thread: How to get temporary folder for current user

Community
  • 1
  • 1
zeebonk
  • 4,864
  • 4
  • 21
  • 31
-2

You should be able to write your own class inherited from Stream class, override its methods (read/write etc), and inside them maintain your own stringbuilder or whatever suits you. Everything will be inside the memory and of course it'll be really fast.

I think XP had "Documents and Settings", vista/7 has Users folder which has its very own place for each user to write temp files at, you should not have a problem writing there. What you are thinking is correct imo.

user734028
  • 1,021
  • 1
  • 9
  • 21
  • Don't invent your own square wheel if a round wheel is already present in the .NET framework. – zeebonk Sep 22 '12 at 12:13
  • actually it wasnt a square wheel, i think you have a problem recognizing wheels? It was a perfectly round wheel, and when you made it yourself (once in a while) instead of wasting your time on social networks and sharing useless information, you found out that you were a better programmer. And yes I agree MemoryStream is absolutely relevant here which actually skipped my mind, my bad. – user734028 Sep 22 '12 at 12:35
  • Interesting aproach! But don't forget about StringBuilder size limit of Int32.MaxValue – Aleksej Vasinov Sep 22 '12 at 12:53