简体   繁体   English

我如何逐行读取texfile并使用C#添加到数组

[英]How can i read texfile line by line and add to array using C#

Am using AfricasTalking C# class to send Airtime. 我正在使用AfricasTalking C#类发送通话时间。 First you create and array of numbers to send airtime. 首先,您创建一个数字数组并发送广播时间。 Below is a code to create Arraylist. 下面是创建Arraylist的代码。 Instead i want to read numbers from a text file and add to rec1. 相反,我想从文本文件中读取数字并将其添加到rec1。

ArrayList AirtimeRecipientsList = new ArrayList();

// Declare hashtable to hold the first number
// Please ensure you include the country code for phone numbers (+254 for Kenya in this case)
// Please ensure you include the country code for phone numbers (KES for Kenya in this case)
// Specify the country currency and the amount as shown below

Hashtable rec1 = new Hashtable();
rec1["phoneNumber"] = txtPhoneNo.Text;
rec1["amount"] = "KES "+ txtAmount.Text;

// Add recipient to list
AirtimeRecipientsList.Add(rec1);

// Declare hashtable to hold the another number
//Hashtable rec2 = new Hashtable();
//rec2["phoneNumber"] = "+254733YYYZZZ";
//rec2["amount"] = "KES XXX";

// Add recipient to list
//AirtimeRecipientsList.Add(rec2);`

Assuming your text file has phone number and amount separated by comma or any character like this "123456789, KES 500" on each line without quotes. 假设您的文本文件的电话号码和数量用逗号或每行中不带引号的任何字符(例如“ 123456789,KES 500”)分隔,并用逗号分隔。 Here is some code. 这是一些代码。

    var recipients = System.IO.File.ReadAllLines(path to text file);
    ArrayList recipientsList = new ArrayList();
     foreach(var line in recipients)
         { 
           Hashtable rec1 = new Hashtable();
          rec1["phoneNumber"] = line.Split(',')[0];
          rec1["amount"] = line.Split(',')[1];
           recipientsList.Add(rec1)
         }

That's it. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM