如果文件中包含字符串,则一切正常。如果文件不包含字符串,我只需要一条消息。 MessageBox未显示或无限循环显示。我尝试了假/真,但它没有用。
TextFieldParser sprawdz = new TextFieldParser("C:\\wykaz_druk.csv");
string currentLine;
string searchcsv = textBox_SPR_SEARCH.Text;
sprawdz.TextFieldType = FieldType.Delimited;
sprawdz.Delimiters = new string[] { ";" };
sprawdz.TrimWhiteSpace = true;
bool check = false;
do
{
currentLine = sprawdz.ReadLine();
if (currentLine != null)
{
check = true;
string file = currentLine;
string serial = file.Split(';')[2].Trim();
string adresip = file.Split(';')[3].Trim();
if (adresip == searchcsv || serial == searchcsv)
{
textBox2.Text = serial;
textBox4.Text = adresip;
}
}
} while (currentLine != null);
if (check == false)
{
MessageBox.Show("Error");
}
我也试过if / else但是它仍然无法工作:
TextFieldParser sprawdz = new TextFieldParser("C:\\wykaz_druk.csv");
string currentLine;
string searchcsv = textBox_SPR_SEARCH.Text;
sprawdz.TextFieldType = FieldType.Delimited;
sprawdz.Delimiters = new string[] { ";" };
sprawdz.TrimWhiteSpace = true;
while (!sprawdz.EndOfData)
{
currentLine = sprawdz.ReadLine();
if (currentLine != null)
{
string file = currentLine;
string serial = file.Split(';')[2].Trim();
string adresip = file.Split(';')[3].Trim();
if (adresip == searchcsv || serial == searchcsv)
{
textBox2.Text = serial;
textBox4.Text = adresip;
}
}
else
{
MessageBox.Show("Error");
break;
}
}
答案 0 :(得分:1)
我怀疑你要分配:
check = true;
在错误的地方。
在此处移动该支票:
if (adresip == searchcsv || serial == searchcsv)
{
check = true; // <-- insert here
textBox2.Text = serial;
textBox4.Text = adresip;
}