Question

I tried the following using C#:

using System.IO;

string[] FileName = Directory.GetFiles("dir");
for (int i = 0; i < dir.Length; i++)
{
    comboBox1.Items.Add(Dirs[i]);
}

But for some reason it only gets 5 files from this folder. Is it possible to get all of the file's names and put them in the combobox?

Thanks in advance.

Was it helpful?

Solution

Problem : You are getting all the FileNames into FileName string array but you are only getting 5 because you are not using FileName string array in your code.

Solution : You need to use FileName String Array instead of dir.

Try This:

string[] FileName = Directory.GetFiles("dir");
for (int i = 0; i < FileName.Length; i++)    
{
   comboBox1.Items.Add(FileName[i]);
}

OR

string [] FileNames = Directory.GetFiles("dir");
foreach (var filename in FileName)    
{
  comboBox1.Items.Add(filename);
}

OTHER TIPS

Probably you are looking for this:

 string[] FileName = Directory.GetFiles("dir","*",SearchOption.AllDirectories);
foreach(string fileName in Directory.GetFiles("dir", "*", SearchOption.AllDirectories))
{
    comboBox1.Items.Add(fileName)); 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top