我正在尝试阅读以下文本文件:(跳过前 8 行)并从箭头读取每一列
我这样做是通过将每个列值放入一个由位置和长度决定的数组中
要测试数组值是否实际捕获了列值,我想在单击另一个按钮时查看值 [0]。但是当我运行我的应用程序时,我得到了我的索引超出数组范围的错误?怎么样,当我的数组大小为 3 并且我不超过这个值时。
string[] val = new string[3 ]; // One of the 3 arrays - this stores column values
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] lines = File.ReadAllLines(ofd.FileName).Skip(8).ToArray();
textBox1.Lines = lines;
int[] pos = new int[3] { 3, 6,18}; //setlen&pos to read specific clmn vals
int[] len = new int[3] {2, 10,28}; // only doing 3 columns right now
foreach (string line in textBox1.Lines)
{
for (int j = 0; j <= 3; j++)
{
val[j] = line.Substring(pos[j], len[j]); // THIS IS WHERE PROBLEM OCCURS
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{ // Now this is where I am testing to see what actual value is stored in my //value array by simply making it show up when I click the button.
MessageBox.Show(val[0]);
}
}
}