[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Populating a combo box with files in a folder

Posted on 2005-04-21
5
Medium Priority
?
654 Views
Last Modified: 2008-02-01
Hi,
I want to be able to populate a combo box with files in my directory(c:\myTemplates). The files are actually word templates which I will show on the combo box. They have an extension .dot and I do not want the extension .dot to show in the combo box.  How can I handle this please using c#(windows application)

Folder structure
C:\MyTemplates\ Template1.dot
                          Template2.dot
                          Template3.dot

combo box should show Template1
                                   Template2
                                   Template3

What is the best way to do this. Is it possible with Xml file???

Thanks.
0
Comment
Question by:Sirdots
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:sharpnet
ID: 13835389
You'll want to use either (probably both) the DirectoryInfo object or the FileInfo object.  They are part of the System.IO namespace.

In a list box, it could look like:

DirectoryInfo theFolder = new DirectoryInfo("c:\\myTemplates");

// list all files in a folder
foreach (FileInfo nextFile in theFolder.GetFiles())
{
   listBox1.Items.Add(nextFile.Name); // you can do some string work here to cut off the extension
}

Hope this helps.

Nick
0
 

Author Comment

by:Sirdots
ID: 13835680
This helps a lot. I appreciate it. I am still having some trouble cutting off the strings from the back i.e getting off  ".dot"

Thanks.
0
 

Author Comment

by:Sirdots
ID: 13835726
I will also prefer to  have the first item as a default value on the combo box.

Thanks.
0
 
LVL 3

Expert Comment

by:sharpnet
ID: 13835753
well, you can do something like:

foreach (FileInfo nextFile in theFolder.GetFiles())
      {
            string[] fileName = nextFile.Name.Split('.'); // note the single quotes, not double quotes.
            listBox1.Items.Add(fileName[0]);
      }

The split function will break the filename into two parts (assuming there is only one dot in the filename) and place each half of the file name into an array.  The first index in the array (0) will be the filename, the second index (1) will be the extension.  So, you can just call fileName[0] to get the first portion of the filename and add it to your list box or combobox...

Nick
0
 
LVL 3

Accepted Solution

by:
sharpnet earned 2000 total points
ID: 13835778
If you're using a combobox, just use

comboBox1.SelectedIndex = 0;

 to make the first item in the list as the default.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

834 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