Visual Studio 2010 ( - Explorer type of window on Wondows form

Murray Brown
Murray Brown used Ask the Experts™

In my VS 2010 project I have a Windows form. I want to put a Windows Explorer type control
that only shows certain file extensions. How would I do this?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Use treeview or showdialog (select one or more files)
Top Expert 2015
An Explorer type control is usually a combination of controls. A SplitContainer, that contains a TreeView in the left portion and a ListView in the right portion. You will need to learn to work with those controls.

To help you, there is already a form that combines them in the right way in the form templates. When you select Add New Item from the Project menu, there is an Explorer Form in the Common Items. It alread has a lot of code, you will probably have to remove some stuff, but it will get you going for the form itself. Look for TODO: comments, they tell you were you should put the code to complete the functions.

The LoadTree and LoadListView methods are probably the ones that interests you the most. It already has codes that shows you how to fill the TreeView and the ListView. Simply replace it by what you want to display there.

To retrieve a list of files with a given extension in a directory, use a System.IO.DirectoryInfo object. It has a GetFiles method that lets your retrieve directories (for the TreeView) and files (for the ListView) inside a directories while providing a filter.

Here is code that would fill the ListView with the Microsoft Word files in the MyDir directory, and also display their last modification date and length :

Dim lvItem As ListViewItem

Dim dir As New System.IO.DirectoryInfo("C:\MyDir")

For Each File As System.IO.FileInfo In dir.GetFiles("*.doc")
  lvItem = ListView.Items.Add(File.Name)
  lvItem.ImageKey = "Graph1" 'Leave that line out if you do not need to display an icon with the file
  lvItem.SubItems.AddRange(New String() {File.LastWriteTime.ToString, File.Length.ToString})

Open in new window

Most Valuable Expert 2012
Top Expert 2014
Murray Developer


thank you all

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial