ProblemYou need to create a new directory to store user or application data. SolutionSample code folder: Chapter 12\NewDirectory Use the My.Computer.FileSystem. CreateDirectory() method to create the new directory. Pass the method a String containing the directory path to create. DiscussionTo try out this feature, create a new Windows Forms application, and add a TextBox control named TextBox1 and a Button control named Button1 to the form. Now add the following code to the form's class template: Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click ' ----- The user must supply a directory. If (Trim(TextBox1.Text) = "") Then MsgBox("Please supply a directory.") TextBox1.Focus( ) Exit Sub End If ' ----- Create the directory requested by the user. If (My.Computer.FileSystem.DirectoryExists( _ TextBox1.Text)) Then MsgBox("The directory already exists.") Else Try My.Computer.FileSystem.CreateDirectory(TextBox1.Text) MsgBox("Directory created successfully.") Catch ex As Exception MsgBox("The directory could not be created due " & _ "to the following error:" & _ vbCrLf & vbCrLf & ex.Message) End Try End If End Sub The CreateDirectory( ) method accepts either absolute or relative paths in drive-letter or UNC format, but not URL-based "file://" paths. If the directory cannot be created, CreateDirectory( ) generates an exception. A variation of this method exists through the System.IO.Directory.CreateDirectory( ) function. This function returns a System.IO.DirectoryInfo object for the newly created directory object. It also includes a second overload that accepts security settings for the new directory. |