I would like to populate my ListBox with whatever items there is in the specific path.
string path = @"C:\users\test\desktop";
So whatever items there are on my desktop, the listbox should have those items in it.
I tried making the ListBox1.ItemSource = path , but all that does is it writes the path to the listbox :(.
Any suggestions or help.
Thanks
Take a look at this article. Specifically, these sections:
Once you setup your project properties, open the XAML file and add a
Buttoncalled “Read File” and attach aClick_Eventto it. We will also add aListBox, so that we can display the text content inside it. Here is the code snippet of the same:Now we need to implement the
Clickevent logic for theButton. To do this, go to the code behind file and write your own logic to read any file present in your system. In our example, we will use theSystem.IO.Fileclass and read the hosts file. Then we will add the content in ourListBox.Here is the code implementation:
Just call the
File.ReadAllText()method and pass the complete file path as the parameter. This will read the content and store it in a local variable calledfileContent. Now add the content to ourListBox.