While I believe OriginalGriff has answered this question correctly, I'll throw this in the pot just to add a slightly different flavor.
1. There's no mystery why your current code sticks the same binary-string value in each array element: you are only passing it one value !
2. Do you assign 'myArr a fixed size of ten elements because that's all you want; and, are you sure the result of the Directory Search will always return at least ten elements ?3. Note: this example uses only one string array, but there's no reason not to use two of them, and it might make the code clearer to use two of them.
private string fileNames;
private string testFilePath = @"C:\temp\test";
private string testFileType = @"*.png";
private void populateArray(string filePath, string fileType)
fileNames = Directory.GetFiles(filePath, fileType, SearchOption.AllDirectories);
catch (DirectoryNotFoundException ex)
MessageBox.Show("Error: " + ex.Message);
for (int j = 0; j < fileNames.Length; j++)
img = (Bitmap)Bitmap.FromFile(fileNames[j]);
fileNames[j]+= " " + convertToBinary(img);
Now, let's take one step back, and consider the possibilities of errors in your using Directory.GetFiles ... we've demonstrated catching one kind of error above: you may want to consider whether to handle one or more of several specific possible errors of this File.IO method ... see: [^
] ... or handle all possible error messages with the same error handler ... but, imho, you should think about the possibilities of errors using a method like GetFiles ! An error I once encountered using the 'AllDirectories option was caused by one sub-folder not having access permission.
What if you find some enormously large number of files that match in a specified directory ? Is that a problem ? Using the techniques here, you cannot just "eat until full," then stop:
So, if all you really want is
ten filepaths, or some other arbitrary number ... ?
Looking one step further: your array of strings where the file name of the file is concatenated with a binary representation of file contents (byte-by-byte) essentially ... assuming you want to use both pieces of information now merged into one array element ... means that to access a given file's binary data, you are going to have take an array element and "split it" in some way: that's very inefficient, and unnecessary.
So, how else might you design your data structure to preserve the linkage between file name and binary representation: a Dictionary<string,string> is one choice that can be used here, since we can assume the complete filepath of any one file is unique, and can be used as the Key for each Dictionary item; naturally, the binary representation would become the Value of the Key for the given Dictionary item.