I'am loading a text file and displaying the textfile name in a listbox. And when I click the listbox im saving the path of the file to a variable. This works in another application of mine, but in the new application I get the following error :
Error 8 The best overloaded method match for 'string.Join(string, string[])' has some invalid arguments Error 9 Argument 2: cannot convert from 'System.Collections.Generic.IEnumerable' to 'string[]'
This is the code :
string fileloadpath;
private void FileListbox_SelectedIndexChanged(object sender, EventArgs e)
{
var selectedItems = FileListbox.SelectedItems.Cast<FileItem>();
var all = string.Join(Environment.NewLine, selectedItems.Select(x => x.Path1));
fileloadpath = all;
}
Edit : I added ToArray() at the end and that fixed it. Thanks guys.
I have one more error related to this :
When I display the file path in the listbox, instead of the actual file name(test.txt), the text is displayed as : "OpenCV.Form1+FileItem"
Here is the code :
void reciperefresh()
{
FileListbox.Items.Clear();
string[] files = Directory.GetFiles(@"C:\Recipe", "*.txt",
SearchOption.AllDirectories);
foreach (string f in files)
{
var fileItem = new FileItem { Title1 = Path.GetFileName(f),
Path1 = Path.GetFullPath(f) };
FileListbox.Items.Add(fileItem);
}
}
fileloadpath?.ToArray()on the end of your query.selectedItems.Select(x => x.Path1)will return anIEnumerable. You need to call.ToArray()on that select