如何向 VB.NET 应用程序添加“浏览到文件”对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3283423/
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 add a Browse To File dialog to a VB.NET application
提问by David
In a VB.NET Windows Forms application how do I add the capability for someone to click a button or image and open a file browser to browse to a file and assign it's path to a variable so I can copy that file to another specific path?
在 VB.NET Windows 窗体应用程序中,我如何添加功能让某人单击按钮或图像并打开文件浏览器以浏览文件并将其路径分配给变量,以便我可以将该文件复制到另一个特定路径?
回答by Sebastian
You should use the OpenFileDialog class like this
你应该像这样使用 OpenFileDialog 类
Dim fd As OpenFileDialog = New OpenFileDialog()
Dim strFileName As String
fd.Title = "Open File Dialog"
fd.InitialDirectory = "C:\"
fd.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
fd.FilterIndex = 2
fd.RestoreDirectory = True
If fd.ShowDialog() = DialogResult.OK Then
strFileName = fd.FileName
End If
Then you can use the File class.
然后你可以使用 File 类。
回答by SLaks
You're looking for the OpenFileDialog
class.
您正在寻找OpenFileDialog
班级。
For example:
例如:
Sub SomeButton_Click(sender As Object, e As EventArgs) Handles SomeButton.Click
Using dialog As New OpenFileDialog
If dialog.ShowDialog() <> DialogResult.OK Then Return
File.Copy(dialog.FileName, newPath)
End Using
End Sub