我在C#中只有大约三天的时间。我试图打开一个关键字文件并让程序将关键字输入到程序列表中。我一直得到一个看起来像这样的字符串。
"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"
但是我试图将列表项输出到如下所示的列表中。
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
这是我到目前为止所拥有的。任何帮助将非常感激。谢谢。
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>();
}
}
}
}
感谢所有精彩的回放,这就是我从每个人收到的答案中获得的新代码。我现在正在获得所需的输出。这段代码是我做我想要实现的目标的最佳方法吗?谢谢你们!
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();
}
}
}
}
我还有一个问题,是否有办法将项目添加到列表中而不添加引号?这是我得到的出路。
&#34;折扣可用&#34;
&#34;折扣可用&#34;
&#34;%折扣&#34;
&#34;%off&#34;
&#34;优惠券&#34;
&#34;优惠券&#34;
&#34;使用优惠券代码&#34;
&#34;使用优惠券代码&#34;
&#34;优惠券代码&#34;
&#34;优惠券代码&#34;
&#34;订单&#34;
&#34;命令&#34;
&#34;订单&#34;
&#34;命令&#34;
&#34;收到您的订单&#34;
&#34;收到您的订单&#34;
&#34;收到付款&#34;
&#34;期待您的订单&#34;
&#34;期待您的订单&#34;
&#34;期待您的订单&#34;&#34;
&#34;收到详细信息&#34;
&#34;收到详细信息&#34;
&#34;收到详细信息&#34;
答案 0 :(得分:4)
您可以使用File.ReadLines()
将每一行作为IEnumerable<string>
中的单独字符串返回。如果您希望这是List
,就像现在一样,只需添加.ToList()
。
正如@mason指出的那样,您需要添加using System.Linq
。
答案 1 :(得分:0)
问题是你正在做File.ReadAllText(path)
,它将所有东西都读成一个字符串。实际上你想要File.ReadLines()
给你一个字符串数组(每行一个)。
答案 2 :(得分:0)
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();
}
}
}
}
您应该使用File.ReadAllLines()
代替File.ReadAllText()
。还可以使用Linq将结果数组转换为通用的字符串列表。
答案 3 :(得分:0)
您正在寻找string[] File.ReadAllLines(string path)
。如果您想要List<string>
而不是string[]
,那么只需在通话结束时添加ToList()
即可。但是,除非您稍后要添加到集合中,否则我不会推荐它,因为这是一个不必要的操作,会对性能产生负面影响。