Working with Directories and Files in .NET
page 2 of 6
by Joydip Kanjilal
Average Rating: 
Views (Total / Last 10 Days): 27505/ 70

Working with directories

A directory, as we learned earlier, is a special type of a file. Both files and directories are stored in the permanent storage device of your computer system, abiding by the protocols as laid out by the file system of the host Operating System. But what is a file system, anyway? A file system is the format and rules and regulations that are set by the host Operating System of your computer system that specifies how the files and directories in your system are organized. The file system of MSDOS is FAT, Windows 9x is VFAT and the same of Windows XP or Windows NT is NTFS and that of UNIX is UFS.

For working with directories in .NET, you have two classes in the .NET Base Class Library. These classes are the Directory class and the DirectoryInfo class and are present in the System.IO namespace. The System.IO namespace contains a list of classes, methods and properties for accessing the files and directories in the file system and working with them. You can use the classes in the System.IO namespace to read and write files, list directories, get file information, and manipulate files and directories. According to MSDN, "The System.IO namespace contains types that allow reading and writing to files and data streams and types that provide basic file and directory support." The System.IO namespace contains the following classes.

·         TextReader

·         TextWriter

·         StringReader

·         StringWriter

·         BinaryReader

·         BinaryWriter

·         StreamReader

·         StreamWriter

·         File

·         FileInfo

·         Directory

·         DirectoryInfo

Let us start our discussion with the Directory class. We will follow this up with a discussion on the DirectoryInfo class. You have a number of static methods in the Directory class for working with directories. All these methods are static methods and they require a security check that in turn verifies whether the user has the necessary permission to access the file system from within the .NET’s managed environment.

The members of the Directory class in the .NET Base Class Library are:

·         Exists

·         CreateDirectory

·         Delete

·         Move

·         SetCreationTime

·         GetCreationTime

·         SetLastAccessTime

·         GetLastAccessTime

·         SetLastWriteTime

·         GetLastWriteTime

·         GetLogicalDrives

·         GetParent

·         GetCurrentDirectory

·         GetDirectoryRoot

·         GetDirectories

·         GetFiles

·         GetFileSystemEntries

The DirectoryInfo class is similar to the Directory class with the exception that it contains only static methods. The methods of the DirectoryInfo class are as follows.

·         Create

·         CreateSubDirectory

·         MoveTo

·         Delete

·         GetFiles

·         GetDirectories

·         GetFileSystemInfos

The following code snippet illustrates how you can use the Directory class to display the list of files and directories.

Listing 1

string[] files = Directory.GetFileSystemEntries(Environment.GetFolderPath
  string[] directories = Directory.GetDirectories (Environment.GetFolderPath
  foreach (string file in files)
foreach (string directory in directories)

View Entire Article

User Comments

No comments posted yet.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2023  |  Page Processed at 2023-09-24 12:51:56 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search