如何在 vb.net 中使用打开文件对话框指定路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11465237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to specify path using open file dialog in vb.net?
提问by FPGA
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?
我需要选择一个文件才能使其工作,但我只想选择一个文件夹。那么有什么解决办法呢?
回答by Steven Doggart
You want to use the FolderBrowserDialog
class instead of the OpenFileDialog
class. You can find more information about it here:
您想使用FolderBrowserDialog
类而不是OpenFileDialog
类。您可以在此处找到有关它的更多信息:
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
回答by APrough
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。
回答by ad48
Dim filedialog As New OpenFileDialog
filedialog.IntialDirectory = Application.StartupPath
filedialog.ShowDialog()
回答by Kingsman Coder
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
回答by Jaime Herrera
Use To:
用于:
Dim openFD As New OpenFileDialog()
Dim Directory As string = openFD.FileName