-6

I uses this code to get all mdf files in a folder

string[] Dbfiles = Directory.GetFiles(c.Path + c.CompanyName + c.ProgramTitle, "*.mdf", SearchOption.TopDirectoryOnly); MessageBox.Show(Dbfiles[Dbfiles.Length-1]);

works fine on windows 7 and 10 x64 but it gives me Index was outside the bounds exception on windows 7 x32 i tried

MessageBox.Show(Dbfiles[0]);

and other stuff but it doesn't work i uses dot net framework 3.5

ForEach fix it

foreach (string file in Dbfiles)
            {
                if (string.IsNullOrEmpty(file))
                {
                    //escape
                    continue;
                }
                else
                {
                    c.MdfFilePath = file;
                    break;
                }
            }
ahmedpio
  • 132
  • 1
  • 10

1 Answers1

0

Directory.GetFiles returns empty string[] and then you try to get element with "-1" index.

Just try to check the length of the array before accessing its elements.

rusbro
  • 56
  • 7
  • i already tried that also with index as "0" still get the same error – ahmedpio Oct 16 '17 at 12:01
  • it's an empty array, no elements at all. ***0*** index - first element. – rusbro Oct 16 '17 at 13:48
  • i know, i can see it had at least one element from the break piont with the path i want still get exception only on windows 7 32bit, 64bit and 10 64bit works fine with the same code. for loop also works when i replace the index number with a variable but also doesn't return any strings from the array but foreach loop sure works with the same codes – ahmedpio Oct 17 '17 at 10:00