0

I would like to create a folder in VB from a textbox input e.g. I have one "browse " button, a textbox1 and a "create folder" buttons, I want to create a folder from browsing to the file system location where the user would like to create the folder and the selected location should be copied to the textbox1 then the user should click the "create folder" button; if the folder doesn't exit, a dialog should say the folder was successfully created if the folder exists it should say, the folder already exists.

All help is very much appreciated. THANK you.

This is the code that I'm trying to write so far:

Imports System.IO
Public Class Form1
Dim FolderName As String
Private Function CreateFolder()
FolderName = TextBox1.Text
My.Computer.FileSystem.CreateDirectory("" & FolderName & "")

If My.Computer.FileSystem.DirectoryExists("" & FolderName & "") = False Then
Throw New Exception("The specified path does not exist.")
Else
If My.Computer.FileSystem.DirectoryExists("" & FolderName & "") Then
Throw New Exception("Could not create the folder because it already exists.")
End If
End Function
Private Sub FolderCreate()
CreateFolder()
If Not My.Computer.FileSystem.DirectoryExists("" & FolderName & "") Then
Throw New Exception("The folder creation failed.")
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles      Button1.Click
FolderCreate()
End Sub
Private Sub browse_Click(sender As Object, e As EventArgs) Handles browse.Click
If (FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
TextBox1.Text = FolderBrowserDialog1.SelectedPath
End If
End Sub
End Class

2 Answers2

0

So when they browse to a directory, are you expecting the user to not use the "Make New Folder" button when the FolderBrowserDialog appears? From your explanation they will navigate to a folder using the FolderBrowserDialog and click the button to create a directory which will always exist (unless they type in an additional folder name into the Textbox).

Imports System.IO

Public Class Form1

Private FolderBrowserDialog1 As New FolderBrowserDialog

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    If (FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
        TextBox1.Text = FolderBrowserDialog1.SelectedPath
    End If
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If My.Computer.FileSystem.DirectoryExists(Me.TextBox1.Text) Then
        MessageBox.Show("The selected directory already exists!")
    Else
        Try
            My.Computer.FileSystem.CreateDirectory(Me.TextBox1.Text)
            MessageBox.Show("The selected directory has been created!")
        Catch ex As Exception
            MessageBox.Show("The directory could not be created!  Error: " & ex.Message, "Error creating directory.", _
                            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End If
End Sub
End Class
Capellan
  • 717
  • 6
  • 15
  • Hi Capellan, you are absolutely right, I don't think I need to create the folder from the textbox but from the FolderBrowserDialog which is much better, I'll drop the "create folder" button, you know what, I'll just use the FolderBrowserDialog dilaog to create the folder and then the selected folder will be displayed on the "textbox1". thanks for your help – Bayindo Lamuka Oct 17 '14 at 13:21
0

This is what I've done after following Capellan's advice, this is the simple code:

 Private Sub Browse_Click(sender As Object, e As EventArgs) Handles Browse.Click
    If (FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
        TextBox1.Text = FolderBrowserDialog1.SelectedPath
    End If
End Sub