Directory.CreateDirectory Method VB.NET

Class
System.IO.Directory

Syntax
Directory.CreateDirectory(path)

path

Use: Required

Data Type: String The path of the new directory

Return Value

None

Description
Creates a new directory

Rules at a Glance

  • pathmust represent a legal path.
  • pathcan be an absolute or a relative path. For example:
  • Directory.CreateDirectory("C:\Temp")
    specifies an absolute path (it begins with a drive's root directory), while:
    Directory.CreateDirectory("..\Chapter2")

    is a relative path that begins from the current directory. Relative paths can make use of the "." and ".." characters, which represent the current directory and the parent of the current directory, respectively

  • The CreateDirectory method creates all directories required to create a specified path. For example, the code:
  • Directory.CreateDirectory("c:\NewDirectory\NewSubDirectory")

    will create theNewDirectoryfolder if it does not exist and then thenewSubDirectoryfolder if it does not exist.

  • pathcan be either a path on the local system, the path of a mapped network drive, or a UNC path.
  • Programming Tips and Gotchas
    The CreateDirectory method does not raise an error if the directory to be created already exists.

    Directory.Delete Method

    Class
    System.IO.Directory

    Syntax
    Directory.Delete(path [,recursive])

    path

    Use: Required

    Data Type: String The path of the folder to delete.

    recursive

    Use: Optional

    Data Type: Boolean

    Indicates whether the folder and its contents are to be deleted if the folder is not empty. Its default value is False.

    Return Value

    None

    Description
    Removes or deletes an existing directory

    Rules at a Glance

  • Ifpathdoes not exist, the method generates a runtime error.
  • Ifrecursiveis set to False (its default value), the directory must be empty to be successfully deleted; otherwise, a runtime error will be generated.
  • Ifrecursiveis set to True, the method will delete not only the final directory inpath, but also of its files and all of its subdirectories, as well as all nested subdirectories and nested files.
  • pathcan be either an absolute path (a complete path from the root directory to the directory whose existence is to be confirmed) or a relative path (starting from the current directory to the path whose existence is to be confirmed).
  • pathcan be either a path on the local system, the path of a mapped network drive, or a UNC path.
  • pathcannot contain wildcard characters.
  • Programming Tips and Gotchas

  • The Delete method permanently deletes directories and their contents. It doesn't move them to the Recycle Bin.
  • Care must be taken when settingrecursiveto True due to the danger of accidentally removing files, especially since the method does not prompt whether it should delete any folders or files.
  • If the user has adequate rights, the source or destination can be a network path or share name. For example:
  • Directory.Delete("\\NTSERV1\d$\RootTwo")
    Directory.Delete("\\RootTest")

    Directory.Exists Method

    Class
    System.IO.Directory

    Syntax
    Directory.Exists(path)

    path

    Use: Required

    Data Type: String The path of the directory whose existence is to be determined

    Return Value
    True if the specified path exists; False otherwise

    Description
    Determines whether a given directory exists

    Rules at a Glance

    • pathcan be either an absolute path (a complete path from the root directory to the directory whose existence is to be confirmed) or a relative path (starting from the current directory to the path whose existence is to be confirmed).
    • pathcan be either a path on the local system, the path of a mapped network drive, or a UNC path.

    Directory.GetCreationTime Method

    Class
    System.IO.Directory

    Syntax

    Directory.GetCreationTime(path)

    path

    Use: Required

    Data Type: String A valid path

    Return Value
    A Date value indicating the creation date and time of the directory

    Description
    Indicates when a given directory was created

    Rules at a Glance

  • path can be either an absolute path (a complete path from the root directory to the directory whose creation time is to be retrieved) or a relative path (starting from the current directory to the directory whose creation time and existence is to be retrieved).
  • path can be either a path on the local system, the path of a mapped network drive, or a UNC path.
  • path cannot contain wildcard characters.
  • Directory.GetDirectories Method

    Class
    System.IO.Directory

    Syntax
    Directory.GetDirectories(path [, searchpattern])

    path

    Use: Required

    Data Type: String A valid path to a directory

    searchpattern

    Use: Optional

    Data Type: String A directory specification, including wildcard characters

    Return Value
    An array of strings, each element of which is the name of a subdirectory

    Description
    Returns the names of the subdirectories in a particular directory

    Rules at a Glance

  • path can be either an absolute path (a complete path from the root directory to the directory whose subdirectories are to be retrieved) or a relative path (starting from the current directory to the directory whose subdirectories are to be retrieved).
  • path can be either a path on the local system, the path of a mapped network drive, or a UNC path.
  • path cannot contain wildcard characters.
  • If searchpattern is specified, the method returns only those directories whose names match the string, which can contain wildcard characters. Otherwise, searchpattern returns the names of all the subdirectories in the target directory specified by path.
  • If the directory specified by path has no subdirectories, or if no directories match searchpattern, an empty array is returned.
  • Example
    The following code displays all subdirectories of c:\ whose names start with the letter P:

    Programming Tips and Gotchas
    Since GetDirectories can return an empty array, you can prevent an array access error in either of two ways: you can iterate the returned array using the For Each...Next construct, or you can retrieve the value of the UBound function, which is -1 in the case of an uninitialized array.

    Directory.GetDirectoryRoot Method

    Class
    System.IO.Directory

    Syntax
    Directory.GetDirectoryRoot(path)

    path

    Use: Required

    Data Type: String A valid path to a directory

    Return Value
    A String containing the name of the root directory of path

    Description
    Returns the name of the root directory of the drive on which path resides (assuming that path is valid). For example, the code:

    Directory.GetDirectoryRoot("c:\program files\accessories")

    returns the string C:\ as the root directory.

    Rules at a Glance

  • path can be either an absolute path (a complete path from the root directory to the target directory) or a relative path (starting from the current directory to the target directory).
  • path can be either a path on the local system, the path of a mapped network drive, or a UNC path. For example, the code:
  • path cannot contain wildcard characters.
  • Directory.GetFiles Method

    Class

    System.IO.Directory

    Syntax

    Directory.GetFiles(path [, searchpattern])
    path

    Use: Required

    Data Type: String A valid path to a directory

    Searchpattern

    Use: Optional

    Data Type: String

    A file specification, including the wildcard characters * and ?

    Return Value
    An array of strings, each element of which contains the name of a file

    Description
    Returns the names of the files in a specified directory

    Rules at a Glance

  • path can be either an absolute path (a complete path from the root directory to the directory whose filenames are to be retrieved) or a relative path (starting from the current directory to the directory whose filenames are to be retrieved).
  • path can be either a path on the local system, the path of a mapped network drive, or a UNC path.
  • path cannot contain wildcard characters.
  • If searchpattern is specified, the method returns only those files whose names match the string, which can contain wildcard characters. Otherwise, the function returns the names of all the files in the path directory.
  • If the directory specified by path has no files, or if no files match searchpattern, an empty array is returned.
  • Example
    The following code displays all files in c:\ that have the extension .txt:

    Programming Tips and Gotchas
    Since GetFiles can return an empty array, you can prevent an array-access error in either of two ways:
    you can iterate the returned array using the For Each...Next construct, or you can retrieve the value of the UBound function, which is -1 in the case of an uninitialized array.

    Directory.GetFileSystemEntries Method

    Class
    System.IO.Directory

    Syntax
    Directory.GetFileSystemEntries(path [, searchpattern])
    path

    Use: Required

    Data Type: String A valid path to a directory

    searchpattern

    Use: Optional

    Data Type: String A file specification, including wildcard characters

    Return Value
    An array of strings, each element of which contains the name of a filesystem entry (that is, a file or
    directory) in the path directory
    Description
    Returns the names of the filesystem entries (that is, of files and directories) in a specified directory
    Rules at a Glance

  • path can be either an absolute path (a complete path from the root directory to the directory whose entries are to be retrieved) or a relative path (starting from the current directory to the directory whose entries are to be retrieved).
  • path can be either a path on the local system, the path of a mapped network drive, or a UNC path.
  • path cannot contain wildcard characters.
  • If searchpattern is specified, the method returns only those filesystem entries whose names match the string, which can contain wildcard characters. Otherwise, the function returns the names of all the filesystem entries in the target directory specified by path.
  • If the directory specified by path has no filesystem entries, or if no filesystem entries match searchpattern, an empty array is returned.

Example
The following code displays all filesystem entries in c:\:

Programming Tips and Gotchas

  • The GetFileSystemEntries method combines the functionality of the GetDirectories and GetFiles methods.
  • Since GetFileSystemEntries can return an empty array, you can prevent an array-access error in either of two ways: you can iterate the returned array using the For Each...Next construct, or you can retrieve the value of the UBound function, which is -1 in the case of an uninitialized array.

Directory.GetLogicalDrives Method

Class
System.IO.Directory

Syntax
Directory.GetLogicalDrives( )

Return Value
An array of strings, each element of which contains the name of the root directory on each logical drive
on a system

Description
Retrieves the names of all logical drives and root directories on a system
Rules at a Glance
In the case of a mapped network drive, GetLogicalDrives returns the letter to which the drive is mapped. For instance, if the folder \\Pentium\C\AFolder is mapped to the Z drive, then GetLogicalDrives will return Z:\ for this logical drive.

Example

On my system, this code displays the following:
A:\
C:\
D:\
E:\
F:\
G:\

Directory.GetParent Method

Class
System.IO.Directory

Syntax
GetParent(path)

path

Use: Required Data Type: String A valid path to a directory

Return Value
A DirectoryInfo object representing the parent directory of path (assuming that path is valid).

Rules at a Glance

  • path can be either an absolute path (a complete path from the root directory to the directory whose filenames are to be retrieved) or a relative path (starting from the current directory to the directory whose filenames are to be retrieved).
  • path can be either a path on the local system, the path of a mapped network drive, or a UNC path.
  • path cannot contain wildcard characters.

Programming Tips and Gotchas
The DirectoryInfo object has properties Name and ToString (among others). The Name property returns only the name of the directory, while the ToString property returns its absolute path. Thus, the following code displays the string program files:

MsgBox(Directory.GetParent("c:\program files\accessories").Name)
whereas the following code displays the string c:\program files:

MsgBox(Directory.GetParent("c:\program files\accessories").ToString)

Directory.Move Method

Class
System.IO.Directory

Syntax
Directory.Move(sourcedirname, destdirname)
sourcedirname

Use: Required

Type: String The name of the directory to be moved

destdirname

Use: Required

Data Type: String The location to which the source drive and its contents are to be moved

Return Value

None

Description
Moves a directory and all its contents, including nested subdirectories and their files, to a new location

Rules at a Glance

  • sourcedirname can be either an absolute path (a fully qualified path from the root directory to the directory to be moved) or a relative path (starting from the current directory to the directory to be moved).
  • sourcedirname and destdirname can be either a path on the local system, the path of a mapped network drive, or a UNC path.
  • Neither sourcedirname nor destdirname can contain wildcard characters.
  • destdirname must be either a fully qualified path or a relative path.
  • destdirname can also be an absolute path or a relative path, except that it must include the name to be assigned to the moved directory. This allows you to rename the directory at the same time as you move it.
  • If the directory indicated by destdirname already exists, an error occurs.

Example
Suppose that the C drive contains the following folders:

c:\docs\letters
c:\Documents and Settings

Moving the letters folder to make it a subdirectory of c:\Documents and Settings is done by the following code:

Directory.Move("c:\docs\letters", _
"c:\Documents and Settings\letters")

Thus, the first argument is the fully qualified name of the folder to move. The second argument is the path that results after the move is made, whereas one might have expected this argument to be the target folder for letters, which is c:\Documents and Settings.

Do...Loop Statement

Syntax

Do [{While | Until} condition] [statements] [Exit Do] [statements] Loop

or:

Do [statements] [Exit Do] [statements] Loop [{While | Until} condition] condition

expression

Use: Optional

Data Type: Boolean expression

An expression that evaluates to True or False

Statements

Use: Optional

Program statements that are repeatedly executed while, or until, condition is True

Description
Repeatedly executes a block of code while or until a condition becomes True

Rules at a Glance

  • On its own, Do...Loop infinitely executes the code that is contained within its boundaries.

You therefore need to specify within the code under what conditions the loop is to stop repeating. In addition, if the loop executes more than once, the variable controlling loop execution must be modified inside of the loop. For example:

Failure to do this results in the creation of an endless loop.

  • Adding the Until keyword after Do instructs your program to Do something Until the condition is True. Its syntax is:

If condition is True before your code gets to the Do statement, the code within the Do...Loop is ignored.

  • Adding the While keyword after Do repeats the code while a particular condition is True. When the condition becomes False, the loop is automatically exited. The syntax of the Do While statement is:

Again, the code within the Do...Loop construct is ignored if condition is False when the program arrives at the loop.

  • In some cases, you may need to execute the loop at least once. You might, for example, evaluate the values held within an array and terminate the loop if a particular value is found. In that case, you would need to execute the loop at least once. To accomplish this, you can place the Until or the While keyword along with the condition after the Loop statement. Do...Loop Until always executes the code in the loop at least once, and continues to loop until the condition is True. Likewise, Do...Loop While always executes the code at least once, and continues to loop while the condition is True. The syntax of these two statements is as follows:

  • A Null condition is treated as False.
  • Your code can exit the loop at any point by executing the Exit Do statement.

Programming Tips and Gotchas
You'll also encounter situations in which you intend to execute the loop continually while or until a
condition is True, except in a particular case. This type of exception is handled using the Exit Do
statement. You can place as many Exit Do statements within a Do...Loop structure as you require. As with any exit from a Do...Loop, whether it is exceptional or normal, the program continues execution on the line directly following the Loop statement. The following code fragment illustrates the use of


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

VB.NET Topics