?
Solved

Populate a ListBox with File Names from a Directory

Posted on 2013-11-18
3
Medium Priority
?
880 Views
Last Modified: 2013-11-19
I have a listbox named lstFiles. and a directory named C:\MyApp\MyFiles.

The directory contains 12 files whose extension is .txt.

How can I populate the listbox with file names in alphabetical order?

Thanks,
0
Comment
Question by:Dovberman
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 39658230
*Assuming WinForms as this really doesn't sound like ASP.Net...

Try something like this:
        private void button1_Click(object sender, EventArgs e)
        {
            string path = @"C:\MyApp\MyFiles";
            var files = (from file in System.IO.Directory.GetFiles(path, "*.txt")
                         orderby System.IO.Path.GetFileNameWithoutExtension(file) ascending 
                         select new
                         {
                             Name = System.IO.Path.GetFileNameWithoutExtension(file),
                             FullPath = file
                         }).ToArray();
            lstFiles.DisplayMember = "Name";
            lstFiles.ValueMember = "FullPath";
            lstFiles.DataSource = files;
        }

        private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstFiles.SelectedIndex != -1)
            {
                label1.Text = lstFiles.SelectedValue.ToString();
            }
        }

Open in new window

0
 

Author Comment

by:Dovberman
ID: 39660187
This is working for asp.net in a web form.

files[2] returns

{ Name = "AMEX_20131017", FullPath = "C:\\Develop\\Apps\\StockPro\\Data\\AMEX_20131017.txt" }
    FullPath: "C:\\Develop\\Apps\\StockPro\\Data\\AMEX_20131017.txt"
    Name: "AMEX_20131017"

Thanks
0
 

Author Closing Comment

by:Dovberman
ID: 39660189
Perfect.

Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question