Home > Back-end >  Searching for files in a Listbox
Searching for files in a Listbox

Time:05-14

I am trying to have an application generate file names then search to see if the files exist. The current issue is, the loop only ever finds the last 2 files on the list.

In my below example screenshots, you can see that if I reorder the files, it still only finds the last 2.

Just for the heck of it, I changed the Listbox to a Textbox to try and loop through, and that gave the same results. Current code is using Listboxes again.

Lastly, the "Store Numbers" box is a textbox that the user fills out. The rest are listboxes.

enter image description here enter image description here

private void findButton_Click(object sender, EventArgs e)
        {
            string dirPath = @"cashFiles\";
            foreach (object file in outputBox.Items) 
            {
                // Search 1
                if (File.Exists(dirPath   "1\\"   file.ToString()))
                {
                    resultBox.Items.Add("Found "   dirPath   @"1\"   file);
                    foundfileBox.Items.Add(dirPath   @"1\"   file);
                }
                else
                {
                    resultBox.Items.Add("Not Found "   dirPath   @"1\"   file);
                }
            }
        }



 private void generateButton_Click(object sender, EventArgs e)
        {
            var textLines = storeNumbers.Text.Split('\n');
            foreach (var line in textLines)
            {
                outputBox.Items.Add("c"   line   datebox.Text   "0.dat");
                outputBox.Items.Add("c"   line   datebox.Text   "1.dat");
            }
        }

CodePudding user response:

Don't simply split at any found \n character. That's the problem.

On Windows, line breaks are typically encoded as \r\n.

So, as an example, in your Store Numbers textbox you have the string "0047\r\n0049".

Doing a \n split will result in the two strings "0047\r" and "0049". Note that the first string contains the \r character.

Thus, all file names generated from this first string also contain the \r character. Obviously, there are no files in your cashFiles directory that possess such an \r character.

You can't see these \r characters in your GUI, because all those controls do not visibly render the character \r. But when you debug your code and carefully inspect the generated file names character-by-character in the debugger, you'll notice the \r in some of the generated file names.

There are different ways of how you could split the string with the Store Numbers correctly regardless of whether it contains Windows-style or Unix/Linux-style line breaks. One simple way is to utilize Environment.NewLine, which provides the character sequence that is used for line breaks by the underlying operating system.

var textLines = storeNumbers.Text.Split(Environment.NewLine);

This relies on the fact that line breaks typed into GUI controls are normally governed by the underlying operating system.

If for any reason the line break character sequences in the string do not match with the line break character sequence as used by the underlying operating system, another relatively simple approach could be:

var textLines = storeNumbers.Text.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  •  Tags:  
  • c#
  • Related