On CBSSports.com: Mike Tyson's daughter dies in accident

List directory contents with VB.NET's DirectoryInfo and FileSystemInfo classes

Tags: Microsoft development tools, Programming languages, Microsoft Visual Basic.Net, Irina Medvinskaya, FileSystemInfo Â, DirectoryInfo, Visual Basic Tips Newsletter

  • Save
  • Print
  • Recommend
  • 1

Takeaway: Irina Medvinskay shows you how to list all the files in a particular directory and then access the files' properties with the VB.NET classes DirectoryInfo and FileSystemInfo.

VB.NET offers two handy classes—DirectoryInfo and FileSystemInfothat allow you to access directories, check their properties, and perform other basic functions on the file system. DirectoryInfo lets you get information about a directory, and create, move, and enumerate through directories and subdirectories. FileSystemInfo has methods that allow you to manipulate both files and directories; therefore, a FileSystemInfo object can represent either a directory or a file. In this tip, I'll show you a way to list all the files in a particular directory and then access the files' properties using DirectoryInfo and FileSystemInfo classes.

Listing A demonstrates a simple way of looping through a particular directory and displaying its contents. I display a name of each file or subdirectory found in a given directory. I also add a label and a list box to the form. The list box will display the contents of the directory. The screen will look like Figure B.

In the code, I define the variable sDir to hold the location of the directory, which we will access in our example. Then I create an instance of the DirectoryInfo class, specifying its location with the dDir variable, creating a loop to go through the directory, and adding each file found in the directory to a list control. In the loop, I utilize an instance of the FileSystemInfo class since the directory may include both files and subdirectories. The result will look similar to Figure C.

Miss a tip?

Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya's column.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

  • Save
  • Print
  • Recommend
  • 1

Print/View all Posts Comments on this article

Edited bobsaget@... | 03/20/07
RE: List directory contents with VB.NET's DirectoryInfo and FileSystemInfo classes rafremiart@... | 06/28/07
RE: List directory contents with VB.NET's DirectoryInfo and FileSystemInfo classes rafremiart@... | 06/28/07
RE: List directory contents with VB.NET's DirectoryInfo and FileSystemInfo classes sjb001@... | 01/23/08
RE: List directory contents with VB.NET's DirectoryInfo and FileSystemInfo classes mike.parks@... | 11/11/08

What do you think?

White Papers, Webcasts, and Downloads

Article Categories

Security
Security Solutions, IT Locksmith
Networking and Communications
E-mail Administration NetNote, Cisco Routers and Switches
CIO and IT Management
Project Management, CIO Issues, Strategies that Scale
Desktops, Laptops & OS
Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
Data Management
Oracle, SQL Server
Servers
Windows NT, Linux NetNote, Windows Server 2003
Career Development
Geek Trivia
Software/Web Development
Web Development Zone, Visual Basic, .NET

SmartPlanet

advertisement
Click Here