我编写了一个程序,从目录中读取XML集合,但我的问题是,当我编写文本文件时,我做了一个测试,我发现了这个我把44 XML文件在列表框和计数是44,但索引是43。程序不能写入的最后一个文件!(
private void button1_Click_1(object sender, EventArgs e)
{
int a = 0;
int total = Convert.ToInt16(ListCount.Text) ;
while ( a < total)
{
string path = Convert.ToString(listBox1.SelectedItem);
XmlSerializer xs = new XmlSerializer(typeof(CAPTURE));
FileStream read = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
CAPTURE cap = (CAPTURE)xs.Deserialize(read);
textBox1.Text = cap.LOAN_CONTROL;
textBox2.Text = cap.LOAN_NUMBER;
textBox3.Text = cap.CLNT_NAME;
textBox4.Text = cap.SOCSEC_NUM;
textBox5.Text = cap.DOC_TYPE;
textBox6.Text = cap.DOC_DATE;
if (textBox1.TextLength == 8 && textBox2.TextLength == 10 && textBox4.TextLength == 9 && textBox5.TextLength == 7 && textBox6.TextLength == 8)
{
string loan = Convert.ToString(int.Parse(textBox1.Text));
string file_name = Global.General.path1 + "\success.txt";
//string file_name = "C:\Users\Lesther\Desktop\test.txt";
System.IO.StreamWriter objWriter;
objWriter = new System.IO.StreamWriter(file_name, true);
objWriter.Write(a + Environment.NewLine);
objWriter.Write(DateTime.Now + Environment.NewLine);
objWriter.Write(textBox1.Text + Environment.NewLine);
objWriter.Write(textBox2.Text + Environment.NewLine);
objWriter.Write(textBox3.Text + Environment.NewLine);
objWriter.Write(textBox4.Text + Environment.NewLine);
objWriter.Write(textBox5.Text + Environment.NewLine);
objWriter.Write(textBox6.Text + Environment.NewLine);
objWriter.Write("******************************************");
objWriter.Write(Environment.NewLine);
objWriter.Flush();
objWriter.Close();
}
else
{
string file_name = Global.General.path1 + "\fail.txt";
//string file_name = "C:\Users\Lesther\Desktop\Fail.txt";
System.IO.StreamWriter objWriter;
objWriter = new System.IO.StreamWriter(file_name,true);
if (textBox1.TextLength < 8)
{
string loan_number = " Error en el Numero de Prestamo";
objWriter.Write(textBox1.Text + loan_number + Environment.NewLine);
}
else
{
objWriter.Write(textBox1.Text + Environment.NewLine);
}
if (textBox2.TextLength < 10)
{
string loan_control = " Error en el Numero Control";
objWriter.Write(textBox2.Text + loan_control + Environment.NewLine);
}
else
{
objWriter.Write(textBox2.Text + Environment.NewLine);
objWriter.Write(textBox3.Text + Environment.NewLine);
}
if (textBox4.TextLength < 9)
{
string socsec_num = " Error en el Seguro Social";
objWriter.Write(textBox4.Text + socsec_num + Environment.NewLine);
}
else
{
objWriter.Write(textBox4.Text + Environment.NewLine);
}
if (textBox5.TextLength < 7)
{
string doc_type = " Error con el Doc Type";
objWriter.Write(textBox5.Text + doc_type + Environment.NewLine);
}
else
{
objWriter.Write(textBox5.Text + Environment.NewLine);
}
if (textBox6.TextLength < 8)
{
string doc_date = " Error con el Doc Date";
objWriter.Write(textBox6.Text + doc_date + Environment.NewLine);
}
else
{
objWriter.Write(textBox6.Text + Environment.NewLine);
}
objWriter.Write("******************************************");
objWriter.Write(Environment.NewLine);
objWriter.Flush();
objWriter.Close();
}
if (a < total)
{
a++;
listBox1.SelectedIndex++;
}
else
{
listBox1.Items.Clear();
button1.Enabled = false;
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
ListCount.Text = null;
this.Refresh();
}
}
}
您的列表框。selecteindex最可能从1开始。
不需要移动selectedIndex,只需使用items集合来获取特定的项。
string path = Convert.ToString(listBox1.items[a]);