Loading String into FileStream

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do I load a string into a FileStream without going to disk?

For example,

string abc = "This is a string";

How do I load abc into a FileStream?

FileStream input = new FileStream(.....);

Reason why I am doing this is that a method that I am calling expects a
FileStream but I would prefer to pass it a Filestream generated from a string
instead of a file since it would be extra overhead to create a file from the
string......
 
Chris Fink said:
How do I load a string into a FileStream without going to disk?

For example,

string abc = "This is a string";

How do I load abc into a FileStream?

FileStream input = new FileStream(.....);

Reason why I am doing this is that a method that I am calling expects a
FileStream but I would prefer to pass it a Filestream generated from a string
instead of a file since it would be extra overhead to create a file from the
string......

If you don't want to involve files, you don't want a FileStream. The
parameter to the method you're calling should almost certainly just be
Stream, not FileStream. If it's not, you're out of luck. If it is, you
should use a MemoryStream which wraps the results of Encoding.GetBytes
in whatever encoding you want to use.
 
Can I cast a MemoryStream to a FileStream?

Jon Skeet said:
If you don't want to involve files, you don't want a FileStream. The
parameter to the method you're calling should almost certainly just be
Stream, not FileStream. If it's not, you're out of luck. If it is, you
should use a MemoryStream which wraps the results of Encoding.GetBytes
in whatever encoding you want to use.
 
Can I cast a MemoryStream to a FileStream?Nope. Where would the FileStream find its FileName property?

But you can cast both to Stream.

Greetings,
Wessel
 
Chris Fink said:
Can I cast a MemoryStream to a FileStream?

No, because it isn't one. A FileStream is always backed by a file -
that's why it's called a FileStream.
 
Back
Top