Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All, new to VB and learning, liking it :). 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" button, 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 exits 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:

VB
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
Posted
Comments
Sinisa Hajnal 17-Oct-14 7:05am    
Do not throw exceptions on expected outcomes. Simply write out the messages, no need to spend resources on creating new exception object simply to show a string message.

What is the problem with your code? You obviously know how to check for the folder existence...why can't you create it?

Try System.IO.Directory namespace...
Member 11155542 17-Oct-14 9:09am    
Sinisa, you are right but the code when run is throwing an Exception around "My.Computer.FileSystem.CreateDirectory("" & FolderName & "")" and I don't know why

I dropped the folder checking and went for the simpler code: Thanks Sanisa
VB
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
 
Share this answer
 
This is what I've come up with:

VB
Dim txt As String
        txt = txtFolderPath.Text
        If CheckBox1.CheckState = 1 Then
            My.Computer.FileSystem.CreateDirectory("" & txt & "\test")
            MessageBox.Show("test folder created successfully, click OK")
        End If
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900