简体   繁体   English

如何在 vb.net 中使用打开文件对话框指定路径?

[英]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\我怎样才能指定一个文件夹而不打开像 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?那么解决方案是什么?

You want to use the FolderBrowserDialog class instead of the OpenFileDialog class.您想要使用FolderBrowserDialog类而不是OpenFileDialog类。 You can find more information about it here:您可以在此处找到有关它的更多信息:

http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog(v=vs.110).aspx 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

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.如果是这种情况,那么您想使用 FolderBrowserDialog 而不是 OpenFileDialog。

Dim filedialog As New OpenFileDialog
filedialog.IntialDirectory = Application.StartupPath
filedialog.ShowDialog()

Or you can simply just make it less lines and very simple.或者你可以简单地让它更少的线条并且非常简单。

link: http://i.imgur.com/bMq0HNz.png链接:http: //i.imgur.com/bMq0HNz.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

Use To:用于:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM