Loading String into FileStream

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Fink

    Loading String into FileStream

    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......


  • KH

    #2
    RE: Loading String into FileStream

    Look up the MemoryStream object


    "Chris Fink" wrote:
    [color=blue]
    > 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......
    >
    >[/color]

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Loading String into FileStream

      Chris Fink <ChrisFink@disc ussions.microso ft.com> wrote:[color=blue]
      > 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......[/color]

      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.GetByt es
      in whatever encoding you want to use.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Chris Fink

        #4
        Re: Loading String into FileStream

        Can I cast a MemoryStream to a FileStream?

        "Jon Skeet [C# MVP]" wrote:
        [color=blue]
        > Chris Fink <ChrisFink@disc ussions.microso ft.com> wrote:[color=green]
        > > 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......[/color]
        >
        > 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.GetByt es
        > in whatever encoding you want to use.
        >
        > --
        > Jon Skeet - <skeet@pobox.co m>
        > http://www.pobox.com/~skeet
        > If replying to the group, please do not mail me too
        >[/color]

        Comment

        • Wessel Troost

          #5
          Re: Loading String into FileStream

          > Can I cast a MemoryStream to a FileStream?[color=blue]
          >[/color]
          Nope. Where would the FileStream find its FileName property?

          But you can cast both to Stream.

          Greetings,
          Wessel

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Loading String into FileStream

            Chris Fink <ChrisFink@disc ussions.microso ft.com> wrote:[color=blue]
            > Can I cast a MemoryStream to a FileStream?[/color]

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

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            Working...