2

I am trying to get streamreader to read a csvfile as a string. I keep getting the argument 1 cannot convert from 'string' to System.IO.Stream. The StreamReader is initializing StreamReader(stream stream) instead of StreamReader(string). Any help would be greatly appreciated.

 public class CsvParser
{

    public static void LoadAndParse()
    {
        string csvData = @"text.csv";
        var fileContents = Parse(csvData);

    }

    public static List<StockInfo> Parse(string csvData)

    {


        var stocks = new List<StockInfo>();
        using (StreamReader reader = new StreamReader(csvData))

        {
            string line = "";
            while ((line = reader.ReadLine()) != null)

            {
                string[] cols = line.Split(',');
                StockInfo s = new StockInfo();
                s.Date = Convert.ToDateTime(cols[0].Trim());
                s.Open = Convert.ToDecimal((cols[1]));
                s.High = Convert.ToDecimal((cols[2]));
                s.Close = Convert.ToDecimal((cols[3]));

                stocks.Add(s);


            }


            return stocks;

        }
    }

}
Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
pelters
  • 29
  • 1
  • 2
  • @EvanTrimboli: that is not a duplicate of this question. This question is asking for help calling the `StreamReader(System.String)` overload, which would normally create a `StreamReader` object while automatically opening a base `FileStream` using the `string` value passed in as a file name. Your proposed duplicate is about something completely different (i.e. creating an in-memory `Stream` that contains the contents of a `string` value). – Peter Duniho Aug 14 '17 at 00:26
  • 1
    To the OP: you appear to be trying to use this code in the context of a UWP project. The UWP version of `StreamReader` requires that you create the underlying `Stream` object explicitly. It does not include the `StreamReader(System.String)` overload found in the desktop .NET Framework API. – Peter Duniho Aug 14 '17 at 00:28
  • @PeterDuniho I really appreciate it. I never in a million years would have figured out that one. – pelters Aug 14 '17 at 01:17

0 Answers0