在我的应用程序的第一次启动时,我需要指定一个路径来保存一些文件。但在打开文件对话框中,似乎我必须选择要打开的文件。如何在不选择文件的情况下指定文件夹 比如C:\ config \
这是我的代码
If apppath = "" Then
Dim fd As OpenFileDialog = New OpenFileDialog()
fd.Title = "Select Application Configeration Files Path"
fd.InitialDirectory = "C:\"
fd.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
fd.FilterIndex = 2
fd.RestoreDirectory = True
If fd.ShowDialog() = DialogResult.OK Then
apppath = fd.FileName
End If
My.Computer.FileSystem.WriteAllText(apppath & "apppath.txt", apppath, False)
End If
我需要选择一个文件才能使其正常工作,但我只想选择一个文件夹。那么解决方案是什么?
答案 0 :(得分:17)
您想要使用FolderBrowserDialog
类而不是OpenFileDialog
类。您可以在此处找到有关它的更多信息:
http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog(v=vs.110).aspx
例如,你可以这样做:
If apppath = "" Then
Dim dialog As New FolderBrowserDialog()
dialog.RootFolder = Environment.SpecialFolder.Desktop
dialog.SelectedPath = "C:\"
dialog.Description = "Select Application Configeration Files Path"
If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
apppath = dialog.SelectedPath
End If
My.Computer.FileSystem.WriteAllText(apppath & "apppath.txt", apppath, False)
End If
答案 1 :(得分:2)
如果我理解正确,您希望让用户选择一个文件夹。如果是这种情况,那么你想使用FolderBrowserDialog而不是OpenFileDialog。
答案 2 :(得分:1)
Dim filedialog As New OpenFileDialog
filedialog.IntialDirectory = Application.StartupPath
filedialog.ShowDialog()
答案 3 :(得分:0)
或者你可以简单地减少线条而且非常简单。
http://i.imgur.com/bMq0HNz.png
'Start your dialog with a click
Private Sub Button1_Click(sender As Object,e As EventArgs)处理Button1.Click FolderBrowserDialog1.ShowDialog() 结束子
'Add if you want to show the actual path that you choose from your dialog
Private Sub FolderBrowserDialog1_Disposed(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = FolderBrowserDialog1.SelectedPath.ToString
End Sub
答案 4 :(得分:0)
用于:
Dim openFD As New OpenFileDialog()
Dim Directory As string = openFD.FileName
答案 5 :(得分:0)
尝试一下
Private Sub BtnOpen_Click(sender As Object, e As EventArgs) Handles BtnOpen.Click
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "x_pathfileforsend"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|*.zip|*.rar|*.ico|*.exe|*.png|*.bmp|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 5
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = DialogResult.OK Then
txtpath.Text = openFileDialog1.FileName
End If
openFileDialog1.Dispose()
End Sub