【问题标题】:Create folder from textbox input in visual basic从 Visual Basic 中的文本框输入创建文件夹
【发布时间】:2014-10-17 10:53:05
【问题描述】:

我想从textbox 输入例如VB 创建一个文件夹我有一个“浏览”按钮、一个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

【问题讨论】:

    标签: vb.net directory creation folderbrowserdialog


    【解决方案1】:

    所以当他们浏览到一个目录时,您是否希望用户在 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
    

    【讨论】:

    • 嗨 Capellan,你说得对,我认为我不需要从文本框创建文件夹,而是从更好的 FolderBrowserDialog 创建文件夹,我将删除“创建文件夹”按钮,你知道吗,我将使用 FolderBrowserDialog 对话框创建文件夹,然后所选文件夹将显示在“textbox1”上。感谢您的帮助
    【解决方案2】:

    这是我在听从 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 2014-03-20
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多