9

In the first start of my application I need to specify a path to save some files to it. But in the open file dialogue it seems like that I have to select a file to open. How can I just specify a folder without oppening a file like C:\config\

Here is my code

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

I need to select a file in order for it to work, but I just want to select a folder. So what's the solution?

Coral Doe
  • 1,925
  • 3
  • 19
  • 36
FPGA
  • 3,525
  • 10
  • 44
  • 73

6 Answers6

19

You want to use the FolderBrowserDialog class instead of the OpenFileDialog class. You can find more information about it here:

http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog(v=vs.110).aspx

For instance, you could do this:

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
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • 2
    The OP asked how to select a path using a *Open File Dialogue*. MS's FolderBrowserDialog is quite useless (as it hasn't been updated since the 90s with folder tree browsing like the old school windows explorer from windows 3.0) and not everyone wants to use it to select paths. – thebunnyrules Jul 08 '17 at 17:36
2

If I understand correctly, you want to let the user choose a folder. If that is the case, then you want to use FolderBrowserDialog instead of OpenFileDialog.

APrough
  • 2,671
  • 3
  • 23
  • 31
1
Dim filedialog As New OpenFileDialog
filedialog.IntialDirectory = Application.StartupPath
filedialog.ShowDialog()
tckmn
  • 57,719
  • 27
  • 114
  • 156
ad48
  • 83
  • 1
  • 1
  • 7
  • Your second line includes a typo in '.IntialDirectory'. The first lower 'i' is missing. The entire line should be 'filedialog.InitialDirectory = Application.StartupPath' – PeterCo Oct 06 '14 at 13:55
0

Or you can simply just make it less lines and very simple.

link: https://i.stack.imgur.com/1CBvr.png

Start your dialog with a click:

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

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
Mark
  • 57
  • 7
0

Use To:

Dim openFD As New OpenFileDialog()
Dim Directory As string = openFD.FileName
0

Try this

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
Fajarsoft
  • 41
  • 4