![]() |
.NET File Processing: Directories |
|
Introduction |
|
A directory is a section of a medium (floppy disc, flash drive, hard drive, CD, DVD, etc) used to delimit a group of files. Because it is a "physical" area, it can handle operations not available on files. In fact, there are many fundamental differences between both:
|
|
The similarities of both types are:
Before using a directory, you must first have it. You can use an existing directory if the operating system or someone else had already created one. You can also create a new directory. Directories are created and managed by various classes but the fundamental class is Directory. Directory is an abstract and sealed class. All of its methods are static, which means you will never need to declare an instance of the Directory class in order to use it. Besides the Directory class, additional operations of folders and sub-folders can be performed using the DirectoryInfo class. To create a directory, you can call the CreateDirectory() method of the Directory class. This method is available in two versions. One of the versions uses the following syntax: public:
static DirectoryInfo ^ CreateDirectry(String ^path);
This method takes as argument the (complete) path of the desired directory. Here is an example: E:\Programs\Business Orders\Customer Information When this method is called:
The Directory::CreateDirectory() method returns a DirectoryInfo object that you can use as you see fit.
Before using or creating a directory, you can first check if it exists. This is because, if a directory already exists in the location where you want to create it, you would be prevented from creating one with the same name. In the same way, if you just decide to directly use a directory that doesn't exist, the operation you want to perform may fail because the directory would not be found. To check whether a directory exists or not, you can call the Directory::Exists() Boolean static method. Its syntax is: public:
static bool Exists(String ^path);
This method receives the (complete) path of the directory. If the path exists, the method returns true. If the directory doesn't exist, the method returns false. To create a directory, you can call the CreateDirectory() method of the Directory class.
One of the most routine operations performed in a directory consists of looking for a file. Both Microsoft Windows operating systems and the user's intuition have different ways of addressing this issue. The .NET Framework also provides its own means of performing this operation, through various techniques. You can start by checking the sub-directories and files inside of a main directory. To look for files in a directory, the DirectoryInfo class can assist you with its GetFiles() method, which is overloaded with three versions. |
|
|
||
| Home | Copyright © 2007 FunctionX, Inc. | Home |
|
|
||