Is it possible to save a remote txt file as a memory stream instead of a file?

Posted on 2007-10-09
Last Modified: 2010-08-05
i.e. saved direct into a memory stream
Question by:stormist
    LVL 19

    Accepted Solution

    This is one way.  It's somewhat inefficient because it creates an unnecessary string.  You can look through the response stream and transder a byte at a time to avoid that.  Or you can just use the response stream directly.

                WebRequest wr = HttpWebRequest.Create("");
                WebResponse response = wr.GetResponse();
                TextReader tr = new StreamReader(response.GetResponseStream());
                MemoryStream ms = new MemoryStream();
                StreamWriter w = new StreamWriter(ms);
                string s = tr.ReadToEnd();
    LVL 10

    Author Comment

    drrichards, thank you for your reply. How would I go about using the response stream directly, assuming I wanted to process it line by line?
    LVL 19

    Expert Comment

    You can just wrap it in a StreamReader:

        StreamReader rdr = new StreamReader(response.GetResponseStream());
        // Read line by line - in a loop, for example
        string line = rdr.ReadLine();
        // loop until line is null

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now