Search code examples
c#.netcompact-framework

"System.IO.File" does not contain a definition for "ReadLines"


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.


Solution

  • 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());
         }
    }