2014-10-17 124 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