List directory contents with VB.NET's DirectoryInfo and FileSystemInfo classes
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 FileSystemInfo—that 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!
Print/View all Posts Comments on this article
|
|
|
|
|
|
|
|
|
|
White Papers, Webcasts, and Downloads
- Why Isn't Server Virtualization Saving Us More? A Few Small Changes May Dramatically Increase Your Efficiency VMware Ever wonder why your company isn't saving more from its server virtualization? Making a few small changes could dramatically increase your efficiency. Download Now
- Email Security and Archiving - Clearer in the Cloud Google The time is NOW for businesses and organizations of all sizes to implement ... Download Now
- Leveraging SMB ERP for an Economic Recovery ZDNet Times are tough but better days are sure to follow. In the wake of an ... Download Now
- VMware Infrastructure: A Guide to Bottom-Line Benefits VMware Frustrated by the high cost of maintaining or building ever-larger data centers? Get the facts you need to formulate your Virtualization Action Plan. Download Now
- Building the Virtualized Enterprise with VMware Iinfrastructure VMware VMware virtualization software has been adopted by over 120,000 enterprise ... Download Now
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

