Sunday, July 12, 2009

Place all filenames in a folder into an array in C#?

I'm using MS Visual C# to develop an Windows Mobile (PocketPC) app and I need to take a listing of all the files in a directory and place them into an array (which I later place into ComboBox options and load the file selected).





I tried this





int count = 0;


string[] list = new string[150];


string current = Directory.GetCurrentDirectory();


string[] files = Directory.GetFiles(current);


foreach (string s in files)


{


list[count] = s;


count++;


}





foreach (string busroute in list)


{


comboBxRoute.Items.Add(busroute);


}





But on every case, it throws a file not found exception. Any tips on a snippet which would work.

Place all filenames in a folder into an array in C#?
//Try this.


//Tested and working.





DirectoryInfo diDirectory;


FileInfo[] fiFiles;


int i;





diDirectory = new DirectoryInfo(@"c:\Windows\");


fiFiles = diDirectory.GetFiles();





if (fiFiles != null)


{


for (i = 0; i %26lt; fiFiles.Length; i++)


{


lbDirectory.Items.Add(fiFiles[i].Name);


}


}


No comments:

Post a Comment