在c#中使用streamreader
本文关键字:streamreader | 更新日期: 2023-09-27 18:24:51
你们是如何实际使用StreamReader
读取.txt文件并与我的组合框的两个文本(如SGD-新加坡元和USD-美元)匹配的,以便它写在显示数字1.26的标签上的?
Exchange.txt:
SGD-新加坡元||美元-美元=1.26
这是代码:
private void GetExchangeRate()
{
using (StreamReader sr = new StreamReader("Exchange.txt"))
{
string[] store = new string[100];
int index = 0;
string line;
while ((line = sr.ReadLine()) != null)
{
store[index] = line;
index++;
lblexchange.Text = sr.ReadLine();
}
}
}
private void tocountry_SelectedIndexChanged(object sender, EventArgs e)
{
btnupdate.Enabled = true;
txtvalue.Enabled = true;
GetExchangeRate();
}
最后,标签没有显示1.26的值。我不知道它出了什么问题。我需要帮助
为什么不直接使用
File.ReadAllLines("Exchange.txt")它会返回字符串数组中的所有行。
你可以这样做
private void GetExchangeRate()
{
string[] lines = File.ReadAllLines("Exchange.txt");
foreach (var line in lines) {
//Suppose your line contains 'Singapore' and you want to do somthing if line contains the singapore then you should do as
if(line.Contains("Singapore"))
{
lblDisplay.Text = "Singapore"
}
//Do your functionality that is which line to display depending upon country
// You can match the line and display them according to your need
}
}