Text Fileto Add in List<T>.

MiPakTeh 1,476 Reputation points
2022-08-13T10:30:14.89+00:00

Hi All,
I have textfile contain 3 lines.

// line1 3518,4745
// line2 9019,0396

// line3 4753,9196

TODO, line1 3518,4745 put in Data2.
line2 9019,0396 put in Data3'
line3 4753,9196 put in Data4'

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.IO;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
  
namespace Str3Number_  
{  
    public partial class Form1 : Form  
    {  
        List<string> Data1 = new List<string>();  
        List<string> Data2 = new List<string>();  
        List<string> Data3 = new List<string>();  
        List<string> Data4 = new List<string>();  
        List<string> Data5 = new List<string>();  
  
        public Form1()  
        {  
            InitializeComponent();  
        }  
  
        private void Form1_Load(object sender, EventArgs e)  
        {  
  
            foreach (var s in File.ReadLines(@"C:\Users\family\Documents\3digit_one.txt"))  
            {  
                listBox1.Items.Add(s);  
                Data1.Add(s);  
            }  
  
        }  






  
  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,096 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,276 Reputation points Microsoft Vendor
    2022-08-17T02:35:43.097+00:00

    @MiPakTeh , Welcome to Microsoft Q&A, based on your description, you want to load the text file into 3 different list.

    I suggest that you could create a list of list<string>, here is a code example you could refer to.
    code.txt

    Result:

    231861-image.png

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful