2014-10-17 79 views
0

我想从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 

回答

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 
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 
+0

嗨Capellan,你是绝对正确的,我不认为我需要创建的文件夹从文本框中,但从FolderBrowserDialog这是好得多,我会放下“创建文件夹”按钮,你知道什么,我将使用FolderBrowserDialog dilaog创建文件夹,然后选定的文件夹将显示在“ TextBox1" 的。谢谢你的帮助 – 2014-10-17 13:21:15