Unable to open CSV file from internet using C#

后端 未结 2 1990
栀梦
栀梦 2021-01-27 13:37

I\'m new with C#. I\'ve written code to open a CSV file from my documents on my local machine. It works well and the data parsing works. Trouble is when I change the code to ope

2条回答
  •  天命终不由人
    2021-01-27 14:21

    Another option is to download data as you're doing, and then wrap it with a MemoryStream:

    WebClient wc = new WebClient();
    byte[] data = wc.DownloadData(
        "http://www.datasource.com/apps/qt/csv/pricehistory.ac?section=yearly_price_download&code=XXX");
    
    using (var ms = new MemoryStream(data))
    {
        using (var reader = new StreamReader(ms))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                // do whatever
            }
        }
    }
    

    The advantage of this over splitting the string is that it uses considerably less memory.

提交回复
热议问题