0

I want to create a file using FileStream and then save that file in a variable for further processing. In my code given below i am giving a path C:/t.doc, but all i want to do is to save that t.doc file in a variable without saving it physically in the machine.

FileStream f = new FileStream("C:/t.doc", FileMode.Create);
Sharon Watinsan
  • 9,620
  • 31
  • 96
  • 140
  • 1
    though the suggest of memoryStream seems a solution, but what you mean by saving in variable, Filestream F is a variable. What you want is to Read from FileStream to MemoryStream, So create memorystream variable and read f object to it. Then you can do whatever you want, and then you can resave it to Filestream. – Sumit Gupta Sep 17 '13 at 10:56
  • in `FileStream ` i am passing in the output path `C:/t.doc`. I don't want to save the file in an output path. – Sharon Watinsan Sep 17 '13 at 10:59
  • FileStream is for reading and/or Writing to File on disk. IF you are neither reading nor writing to disk, then use Stream or MemoryStream instead of this. – Sumit Gupta Sep 17 '13 at 12:08

1 Answers1

3

Instead of using FileStream, use a different Stream class. For example MemoryStream.

The abstraction that the Stream class provides means that you can substitute different subclasses of Stream without needing to change the code that writes to that stream. At least you can do that if your writing code is well-designed and accepts Stream objects rather than, say, FileStream objects. Even if your writing code assumes the use of FileStream it is usually trivial to change it to accept the more general Stream instead.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490