Solved

Populate a ListBox with File Names from a Directory

Posted on 2013-11-18
3
739 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 85

Accepted Solution

by:
Mike Tomlinson earned 500 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now