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
- Volume Activation Planning Guide Microsoft Volume Activation helps Volume Licensing customers automate and manage the ... Download Now
- Live Webcast: Oracle Business Intelligence for Midsize Companies: More Than Just Pretty Dashboards Oracle Oracle's Business Intelligence solutions are widely recognized as market ... Download Now
- Twelve Ways to Reduce Costs with Microsoft(r) SQL Server(r) 2008 Microsoft Many organizations are finding themselves having to deal with difficult ... Download Now
- Dynamic Virtual Clients Intel Intel IT plans to put virtualization on their client PCs - Dynamic Virtual ... Download Now
- Fundamentals of Volume Activation Microsoft Volume Activation is a set of activation methods applicable to systems ... 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
