从visual basic中的文本框输入创建文件夹

时间:2014-10-17 10:53:06

标签: vb.net directory creation folderbrowserdialog

我想从VB输入中textbox创建一个文件夹,例如我有一个“浏览”按钮,一个textbox1和一个“创建文件夹”按钮,我想创建一个文件夹,从浏览到用户想要创建文件夹的文件系统位置,所选位置应该是复制到textbox1然后用户应单击“创建文件夹”按钮;如果文件夹没有退出,则对话框应该说文件夹已成功创建,如果该文件夹存在应该说,该文件夹已经存在。

非常感谢所有帮助。谢谢。

这是我到目前为止要编写的代码:

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 个答案:

答案 0 :(得分:0)

因此,当他们浏览目录时,您是否希望用户在出现FolderBrowserDialog时不使用“创建新文件夹”按钮?根据您的解释,他们将使用FolderBrowserDialog导航到一个文件夹,然后单击按钮创建一个始终存在的目录(除非他们在文本框中键入其他文件夹名称)。

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

答案 1 :(得分:0)

这是我按照Capellan的建议完成的,这是简单的代码:

 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