如何在vb.net中使用打开文件对话框指定路径? [英] How to specify path using open file dialog in vb.net?
问题描述
在我的应用程序的第一次启动中,我需要指定一个路径来向其中保存一些文件.但是在打开文件对话框中,似乎必须选择要打开的文件.如何仅指定文件夹而不添加文件 像C:\ config \
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\
这是我的代码
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?
推荐答案
您要使用FolderBrowserDialog
类而不是OpenFileDialog
类.您可以在此处找到有关它的更多信息:
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
这篇关于如何在vb.net中使用打开文件对话框指定路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!