9

在我的应用程序的第一次启动中,我需要指定一个路径来保存一些文件。但是在打开文件对话框中,我似乎必须选择一个文件来打开。如何只指定一个文件夹而不打开像 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

我需要选择一个文件才能使其工作,但我只想选择一个文件夹。那么解决方案是什么?

4

6 回答 6

19

您想使用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
于 2012-07-13T12:00:48.460 回答
2

如果我理解正确,您想让用户选择一个文件夹。如果是这种情况,那么您想使用 FolderBrowserDialog 而不是 OpenFileDialog。

于 2012-07-13T12:00:34.187 回答
1
Dim filedialog As New OpenFileDialog
filedialog.IntialDirectory = Application.StartupPath
filedialog.ShowDialog()
于 2013-06-01T12:31:41.983 回答
0

或者你可以简单地让它减少线条并且非常简单。

链接:http: //i.imgur.com/bMq0HNz.png

单击即可开始您的对话:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    FolderBrowserDialog1.ShowDialog()
End Sub

如果要显示从对话框中选择的实际路径,请添加

Private Sub FolderBrowserDialog1_Disposed(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox1.Text = FolderBrowserDialog1.SelectedPath.ToString
End Sub
于 2015-03-22T07:19:17.800 回答
0

用于:

Dim openFD As New OpenFileDialog()
Dim Directory As string = openFD.FileName
于 2019-11-25T18:15:06.703 回答
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
于 2020-07-08T20:46:37.740 回答