The System.IO.Directory Class

To access directories you take advantage of the System.IO.Directory class that offers shared members that allow performing common operations on folders. All members are self-explanatory. For example, you can check if a directory already exists and, if not, create a new one as follows:

image

The Move method allows moving a directory from one location to another; it takes two arguments, the source directory name and the target name:

Directory.Move("C:Test", "C:Demo")

If the target directory already exists, an IOException is thrown. You can easily get or set attribute information for directories invoking special methods. For example you can get the directory creation time invoking the GetCreationTime method that returns a Date type or the SetCreationTime that requires a date specification, to modify the creation time:

Dim createdDate As Date = Directory.GetCreationTime("C:Demo")
Directory.SetCreationTime("C:Demo", New Date(2009, 5, 10))

Table 19.1 summarizes members for getting/setting attributes.

Table 19.1 Members for Getting/Setting Attributes

image

You can easily get other information, such as the list of files available within the desired directory. Until .NET Framework 3.5 SP 1 you were limited in invoking the GetFiles method, returning an array of string (each one is a filename), which works like this:

'Second argument is optional, specifies a pattern for search
Dim filesArray() As String = Directory.GetFiles("C:", "*.exe")

Now in .NET 4.0 you can invoke the EnumerateFiles that returns an IEnumerable(Of String) and that works like this:

image

Probably this difference does not make much sense at this particular point of the book, but you learn later that IEnumerable objects are LINQ-enabled; therefore, you can write LINQ queries against sequences of this type. Similarly to GetFiles and EnumerateFiles, you invoke GetDirectories and EnumerateDirectories to retrieve a list of all subdirectories’ names within the specified directory. Next, the GetFilesEntries and EnumerateFilesEntries return a list of all filenames and subdirectories names within the specified directory.

Tip

An important difference in using new methods in .NET 4.0 is that they start enumerating as the Framework is still gathering files/directories, making things faster and more efficient.

To delete a directory you simply invoke the Delete method. Notice that it works only if a directory is empty and simply requires the directory name:

'Must be empty
Directory.Delete("C:Demo")

If the folder is not empty, an IOException is thrown. Delete has an overload that accepts a Boolean value if you want to delete empty subdirectories, too. The Directory class also provides the ability of retrieving a list of available drives on your machine. This is accomplished by invoking the GetLogicalDrives method that returns an array of String that you can then iterate:

image

On my machine the preceding code produces the following output:

image

The last example is about retrieving the current directory, which is accomplished by invoking the GetCurrentDirectory method:

Dim currentFolder As String = Directory.GetCurrentDirectory

You can also easily set the current folder by invoking the shared SetCurrentDirectory method, passing the folder name as an argument. Accessing directories via the Directory class is straightforward, but in some circumstances you have no access to specific information. For this, there is a more flexible class that allows working on specific directories: DirectoryInfo.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.14.118