Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify path using open file dialog in vb.net?

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?

like image 627
FPGA Avatar asked Jul 13 '12 06:07

FPGA


2 Answers

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
like image 83
Steven Doggart Avatar answered Sep 22 '22 16:09

Steven Doggart


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.

like image 37
APrough Avatar answered Sep 20 '22 16:09

APrough