user3605708
user3605708

Reputation: 11

Load File, Read To A List

I'm very new to C# only about three days in. I'm trying to open a file of keywords and have the program enter the keywords into a list in the program. I keep getting a string that looks like this.

"Discount Available\r\nDiscounts Available\r\n% OFF\r\n% off\r\nCoupon\r\ncoupon\r\nUse Coupon Code\r\nuse coupon code\r\ncoupon code\r\nCoupon Code\r\nOrders\r\norders\r\nOrder\r\norders\r\nreceived your order\r\nReceived Your Order\r\npayment received\r\nPayment Received\r\nLooking forward to your order's\r\nlooking forward to your order's\r\nLooking Forward To Your Order's\r\nReceived details\r\nreceived details\r\nReceived Details"

But I'm trying to get the list items to output into a list like this below.

Discount Available
Discounts Available
% OFF
% off
Coupon
coupon
Use Coupon Code
use coupon code
coupon code
Coupon Code
Orders
orders
Order
orders
received your order
Received Your Order
payment received
Payment Received
Looking forward to your order's
looking forward to your order's
Looking Forward To Your Order's
Received details
received details
Received Details

This is what I have so far. Any help would be much appreciated. Thank you.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;

namespace Keywords
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        OpenFileDialog ofd = new OpenFileDialog();

        public void button1_Click(object sender, EventArgs e)
        {
            ofd.Filter = "TXT|*.txt";
            if (ofd.ShowDialog() == DialogResult.OK)
            {

                textBox2.Text = ofd.FileName;
                string filePath = ofd.FileName;

                string path = ofd.FileName;
                string readText = File.ReadAllText(path);

                List<string> fileItems = new List<string>();
                fileItems.Add(readText);

                foreach (string itemfile in fileItems)
                {

                }
                fileItems = new List<string>();
            }

        }

    }
}

Thank you for all your great replays, This is what I have for my new code from the answers I received from everyone. I'm getting the desired output now. Is this code the best method for doing what I'm trying to achieve? Thank you all!

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace Keywords
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        OpenFileDialog ofd = new OpenFileDialog();

        public void button1_Click(object sender, EventArgs e)
        {
            ofd.Filter = "TXT|*.txt";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string path = ofd.FileName;
                List<string> fileItems = File.ReadAllLines(path).ToList();
            }
        }
    }
}

I have one more question, is there a way to add the items to the list without the quotes? This is the out put I'm getting.

"Discount Available"
"Discounts Available"
"% OFF" "% off" "Coupon"
"coupon"
"Use Coupon Code"
"use coupon code"
"coupon code"
"Coupon Code"
"Orders"
"orders"
"Order" "orders"
"received your order"
"Received Your Order"
"Payment Received"
"Looking forward to your order's"
"looking forward to your order's"
"Looking Forward To Your Order's"
"Received details"
"received details"
"Received Details"

Upvotes: 1

Views: 148

Answers (4)

user27414
user27414

Reputation:

You can use File.ReadLines() to return each line as a separate string in an IEnumerable<string>. If you want that to be a List, like you have now, just add .ToList().

As @mason points out, you'll need to add using System.Linq.

Upvotes: 4

evanmcdonnal
evanmcdonnal

Reputation: 48134

You're looking for string[] File.ReadAllLines(string path). If you want a List<string> instead of a string[] then just add ToList() to the end of the call. However, unless you're going to later add to the collection I wouldn't recommend it as it is an unneeded operation that will have a negative impact on performance.

Upvotes: 0

mason
mason

Reputation: 32719

using System;
using System.Linq; //note we import the Linq namespace to we can use .ToList()
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;

namespace Keywords
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        OpenFileDialog ofd = new OpenFileDialog();

        public void button1_Click(object sender, EventArgs e)
        {
            ofd.Filter = "TXT|*.txt";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = ofd.FileName;
                string filePath = ofd.FileName;
                string path = ofd.FileName;
                List<string> fileItems = File.ReadAllLines(path).ToList();                   
            }
        }
    }
}

You should use File.ReadAllLines() instead of File.ReadAllText(). Also use Linq to convert the resulting array to a generic list of strings.

Upvotes: 0

Gigi
Gigi

Reputation: 29521

The problem is that you're doing File.ReadAllText(path) which reads everything into a single string. You actually want File.ReadLines() which gives you an array of strings (one for each line).

This blog post that I wrote last year illustrates exactly how it is used.

Upvotes: 0

Related Questions