如何从visual basic中的文本框输入创建文件夹 [英] How Do I Create a folder from textbox input in visual basic
问题描述
大家好,VB和学习新手,喜欢它:)。我想从文本框输入中在VB中创建一个文件夹,例如我有一个浏览...按钮,一个textbox1和一个创建文件夹按钮,我想创建一个文件夹,从浏览到用户想要创建文件夹的文件系统位置,所选位置应该复制到textbox1然后用户应该单击创建文件夹按钮,如果文件夹没有退出对话框应该说文件夹已成功创建,如果该文件夹存在应该说,该文件夹已存在。非常感谢所有帮助。谢谢。
这是我到目前为止所写的代码:
< pre lang =vb> Imports System.IO
Public 类 Form1
Dim FolderName As 字符串
私有 功能 CreateFolder()
FolderName = TextBox1.Text
My.Computer.FileSystem.CreateDirectory( & FolderName& )
如果 My.Computer.FileSystem.DirectoryExists( & FolderName& )= False 然后
投掷 新异常( 指定的路径不存在。)
Else
如果 My.Computer.FileSystem.DirectoryExists( & FolderName& )然后
抛出 新异常( 无法创建文件夹,因为它已存在。)
结束 如果
结束 功能
私有 Sub FolderCreate()
CreateFolder()
如果 不 My.Computer.FileSystem.DirectoryExists( & FolderName& )然后
投掷 新异常( 文件夹创建失败。)
结束 如果
结束 < span class =code-keyword> Sub
私有 Sub Button1_Click( ByVal sender As System。 Object ,< span class =code-keyword> ByVal e As System.EventArgs)句柄 Button1 .Click
FolderCreate()
结束 Sub
私有 Sub browse_Click(发件人 As Object ,e As Eve ntArgs)句柄 browse.Click
如果(FolderBrowserDialog1.ShowDialog()= Windows.Forms。 DialogResult.OK)然后
TextBox1.Text = FolderBrowserDialog1.SelectedPath
结束 < span class =code-keyword>如果
结束 Sub
结束 类
我删除了文件夹检查并选择了更简单的代码:谢谢Sanisa
私有 Sub Browse_Click(发件人作为 对象, e As EventArgs)句柄 Browse.Click
如果(FolderBrowserDialog1.ShowDialog()= Windows.Forms.DialogResult.OK)然后
TextBox1.Text = FolderBrowserDialog1.SelectedPath
结束 如果
结束 Sub
这就是我想出来的:
Dim txt As 字符串
txt = txtFolderPath.Text
如果 CheckBox1.CheckState = 1 然后
My.Computer.FileSystem.CreateDirectory( & txt& \ test)
MessageBox.Show( 测试文件夹创建成功,单击确定)
结束 < span class =code-keyword>如果
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:
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
I dropped the folder checking and went for the simpler code: Thanks Sanisa
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
This is what I've come up with:
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
这篇关于如何从visual basic中的文本框输入创建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!