string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Admin\Desktop\ID.csv");

string sPattern = idBox.Text;

foreach (string s in lines)
{
    if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
    {
        nameLabel.Text = "test success";
    }
    else
    {
        nameLabel.Text = "test failure";
    }
}

This is basically what I have so far. I am using Visual C#, which is probably obvious, but just in case you didn't know.

ID.csv includes:
123456a, 123457b, 123458c

I want to separate the IDs into separate lines, and be able to search within the string.
If someone enters 456, I would like nameLabel.Text to output the entire line from within the string[]. I know i will need line.Split(',') somewhere, I've read that multiple places but am not sure on how to implement it. Also, it always outputs "test failure" to the nameLabel no matter if what i entered was correct or not. Any help would be appreciated.

Edited 4 Years Ago by zck17: n/a

You might not need to split it if you're using the entire line as output -- just use Contains()
...or are you saying you would ONLY want 123456a if someone enters 456?

Edited 4 Years Ago by thines01: clarity

Then yes, just call Split at the end of the ReadAllLines() using commas as the delimiter. Then you can just search the array.

You can also convert that to a List<string> if you're familiar with that to make it easier to search.

...point of clarity
I'm a linq freak, so sometimes I mention shortcuts that require a litle more explanation.

I would load a List<string> of the items in the CSV like this:

//using System.Linq;
//private static List<string> lst_strKeys = new List<string>(); //possibly at class level

      private static bool LoadFileGetLines(List<string> lst_strLines, string strFileName, ref string strError)
      {
         bool blnRetVal = true;

         try
         {
            System.IO.File.ReadAllLines(strFileName).ToList().ForEach(str =>
               str.Split(',').ToList().ForEach(s => lst_strLines.Add(s.Trim())));
         }
         catch (Exception exc)
         {
            blnRetVal = false;
            strError = exc.Message;
         }

         return blnRetVal;
      }

I would load that outside of where the user makes a choice so the file is not loaded on every choice (like on the page load).

if (!LoadFileGetLines(lst_strKeys, "../../ID.csv", ref strError))
{
   System.Diagnostics.Debug.WriteLine("Could not load input file: " + strError);
   // You can display that error to the user or take whatever necessary action.
   return;
}

To then get data from that list based on substrings, I would do this:

//strUserText comes from your user control (textbox or other)
string strFoundData = lst_strKeys.Where(s => s.Contains(strUserText)).FirstOrDefault();

//then evaluate if it was actually found and mention it to the user
if (!string.IsNullOrEmpty(strFoundData))
{
   nameLabel.Text = strFoundData;
}
else
{
   nameLabel.Text = "not found";
}

If this is confusing, please let me know.

The .csv file I used was this:

123456a, 123457b, 123458c
123556a, 123557b, 123558c
123656a, 123657b, 123658c

Edited 4 Years Ago by thines01: formatting

This article has been dead for over six months. Start a new discussion instead.