我正在尝试读取由逗号和两行分隔的 .txt 文件,目前我已将其设置为读取 txt 文件,但在将 .split 方法与 taxArray 一起使用时遇到问题,我需要使用 .split 所以我可以计算出不同的值。
string[] taxArray = new string[2];
int count = 0;
try
{
if(File.Exists(filePath))
{
//read the lines from text file add each line to its own array index.
foreach (string line in File.ReadLines(filePath, Encoding.UTF8))
{
taxArray[count] = line;
txtDisplay.Text += taxArray[count] + "\r\n";
count++;
}
}
else
{
MessageBox.Show("This file cannot be found");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
这目前完全按照我的需要输出。
这是我的解决方案,我在 foreach 循环完成读取 txt 文件的工作之前调用了数组,感谢 Toby <3 的帮助
string[] taxArray = new string[2];
int count = 0;
// string incomeBracket = taxArray[0];
//string[] incomeBracketArray = incomeBracket.Split(',');
try
{
if(File.Exists(filePath))
{
//read the lines from text file add each line to its own array index.
foreach (string line in File.ReadLines(filePath, Encoding.UTF8))
{
taxArray[count] = line;
txtDisplay.Text += taxArray[count] + "\r\n";
count++;
}
string[] incomeBracket = taxArray[1].Split(',');
txtDisplay.Text = incomeBracket[0];
else
{
MessageBox.Show("This file cannot be found");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句