0

Everything works if in the file is a string. I just need a message if the file doesn't contain string. The MessageBox is not showing up or shows in an endless loop. I tried false/true but it doesn't work.

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");
}

I also tried if/else but it still doesn't work:

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;
    }
}
CDspace
  • 2,639
  • 18
  • 30
  • 36
Przetczak
  • 119
  • 1
  • 11
  • Have a look at this: https://stackoverflow.com/questions/910873/how-can-i-determine-if-a-file-is-binary-or-text-in-c – Marco Nov 01 '17 at 20:09
  • 1
    when you say the file is not string, you mean...like a binary file? If so, then you should use streams – Sparrow Nov 01 '17 at 20:18
  • @Sparrow No. It's work if "string searchcsv = textBox_SPR_SEARCH.Text" is in CSV file. Have problem If what I'm looking for isn't in file. – Przetczak Nov 01 '17 at 20:25
  • @mjwills Everything is fine. No no, something like CSV contains only IP adresses and trying to find something different. – Przetczak Nov 01 '17 at 20:33
  • @mjwills How could I miss that? It's works. Thank you :) – Przetczak Nov 01 '17 at 20:51
  • Why are you using the `TextFieldParser` class but not actually *using* it? Don't manually split on the semi colon, use `ReadFields()` (https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.readfields(v=vs.110).aspx) to get back an already split out array that respects your delimiter and quote settings (not shown in intellisense: https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.hasfieldsenclosedinquotes(v=vs.110).aspx) – pinkfloydx33 Nov 02 '17 at 00:40

1 Answers1

1

I suspect you are assigning:

check = true; 

in the wrong place.

Move that check here instead:

if (adresip == searchcsv || serial == searchcsv)
{
    check = true;    // <-- insert here
    textBox2.Text = serial;
    textBox4.Text = adresip;
}
mjwills
  • 23,389
  • 6
  • 40
  • 63