0% found this document useful (0 votes)
3 views

complete-reference-vb_net_45

Uploaded by

khalid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

complete-reference-vb_net_45

Uploaded by

khalid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Basic File Class Operations

checks the last time the file directory activity status file was written to and, if a certain number of hours have
passed, re−creates the status file and then resets its creation time (see the section "FileSystemWatcher," later
in the chapter).

Moving Files Around

The Move method moves a file to a new location. The method also provides the option of changing the
filename, as demonstrated in the following code:

File.Move(SourceFile, TargetFile)

The arguments SourceFile and TargetFile provide File.Move with source and target path and file names. The
Move method throws exceptions if it cannot source the file or the destination already contains a file of the
same name as the one being moved.

Directory

The Directory class contains static methods exposed for the purpose of creating, moving, and enumerating
through directories and subdirectories. As is the case with the File class, Directory is a shared operations
class. If you need to perform folder operations via an object, then you can use the DirectoryInfo class,
discussed shortly. Table 15−16 lists the members of the Directory class.

Note Malformed path strings will cause exceptions. Refer to "The File Class" and "Path" earlier in
this chapter. Both sections provide specifics to ensure you pass well−formed path strings to
these methods.

The static methods of Directory are straightforward, so I am not going to cover each method with its own
example. The following code, however, parses a given directory and then reports what it finds to the console:

Public Shared Sub ProcessDirectory(ByVal targetDir As String)


Dim subdirectory As String
Dim fileName As String
Dim subdirectories As String() = Directory.GetDirectories(targetDir)
Dim files As String() = Directory.GetFiles(targetDir)
For Each fileName In files
PrintFileInfo(fileName)
Next fileName
For Each subdirectory In subdirectories
ProcessDirectory(subdirectory)
Next subdirectory
End Sub

The ProcessDirectory method starts off taking the path of a target directory passed to it and then it
recursively enumerates all subdirectories in the target directory. The full path is then written to the console
using the PrintFileInfo method in the following code:

Public Shared Sub PrintFileInfo(ByVal path As String)


Console.WriteLine("Found: {0}", path)
End Sub

Table 15−16: The Static Members of the Directory Class

529

You might also like