-1

Here I have a csv file of data, 8 strings every line. I need to read it out from csv and display it on screen through a ListView controls. I created a List<string[]> for data adding to ListView control. And I need to read data from csv into List. The system I used is a industrial HMI, which use WinCE OS, and vendor claims that it support .NET Compact Framework totally. The problem I encountered is, when I use File.ReadLines(path) to read a line form csv file, error occurs when compiling, and the message show "System.IO.File" does not contain a definition for "ReadLines"

I also tried StreamReader, same problem.

namespace Neo.ApplicationFramework.Generated
{
    using System.Windows.Forms;
    using System;
    using System.Drawing;
    using Neo.ApplicationFramework.Tools;
    using Neo.ApplicationFramework.Common.Graphics.Logic;
    using Neo.ApplicationFramework.Controls;
    using Neo.ApplicationFramework.Interfaces;
    using System.Collections.Generic;
    using System.Reflection;

    using System.Collections; 
    using System.IO; 
    using System.Linq;

    public partial class dmScr
    {       
        public List<string> file = new List<string>();
        public List<string[]> inforead = new List<string[]>();
        void fileload_Click(System.Object sender, System.EventArgs e)
        {

            string fileName = (CB_filelist.SelectedItem != null) ? 
                GetStorageCard() + CB_filelist.SelectedItem.ToString() :
                "";
            if(fileName != null && fileName != "")
            {
                fileRead(fileName);
            }
            LV_event.Items.Clear();
            inforead.ForEach(x => 
                {
                ListViewItem lvi = new ListViewItem(x);
                LV_event.Items.Add(lvi);
                });
        }
        private void fileRead(string fileName)
        {
            foreach(string[] item in File.ReadLines(fileName))
                inforead.Add(s);
        }

    }
}

The industrial HMI vendor is Beijer, would be familiar if you're working in related field.

shizhen
  • 12,251
  • 9
  • 52
  • 88
sxjice
  • 1
  • 1
  • 4
  • 1
    `string[] item` should likely be `string item`. – mjwills Jun 18 '19 at 02:14
  • You might have to use `StreadReader`, `ReadLine`, however that's a complete guess – TheGeneral Jun 18 '19 at 02:16
  • @mjwills but it should be ```c# using System; using System.IO; using System.Linq; ``` and I use string[] item because there're 8 strings every line, and there're 8 items every culumn in ListView control. – sxjice Jun 18 '19 at 02:25
  • @TheGeneral Yes I tried, but another problem is , StreamReader return chars not string – sxjice Jun 18 '19 at 02:28

1 Answers1

4

I am not sure CE supports File.ReadLines (not completely sure, don't quote me on this)

However StreamReader has a method called StreamReader.ReadLine

Example

using (StreamReader sr = new StreamReader(path)) 
{
     while (sr.Peek() >= 0) 
     {
         Console.WriteLine(sr.ReadLine());
     }
}
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • I'm wondering StreamReader.ReadLine returns string or string[]. For the data I read, each item have 8 columns. So every time ReadLine executed, does it read out 8 strings or combine 8 into 1? Because I need to fill these every 8 strings into a ListView control. Anyway, there should be some method to solve this. And than you very much. – sxjice Jun 18 '19 at 02:45
  • @sxjice it reads one line at a time, add them to `inforead.Add(` – TheGeneral Jun 18 '19 at 02:47
  • ```C# private void fileRead(string fileName) { using(StreamReader sr = new StreamReader(fileName)) { while(sr.Peek() >= 0) { inforead.Add(sr.ReadLine()); } } } ``` and "Cannot be Converted from string to string[]" – sxjice Jun 18 '19 at 03:04
  • I assume you want to split by something `inforead.Add(sr.ReadLine().Split(','))` – TheGeneral Jun 18 '19 at 03:07
  • I've just figured it out, and you post this. Thank you very much. – sxjice Jun 18 '19 at 03:12
  • BTW, where can I get .NET Compact methods references? – sxjice Jun 19 '19 at 00:28
  • @sxjice i honestly don't know – TheGeneral Jun 19 '19 at 00:41