vb.net ~ 如何将文件夹名称从目录添加到列表框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24510702/
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
vb.net ~ How to add folder names from a directory to a list box
提问by user3688529
I'm trying to add folder names from within a directpory specified by the user to a list box. I have tried a few solutions, but can't seem to add any items. Most recently I tried:
我正在尝试将用户指定的目录中的文件夹名称添加到列表框中。我尝试了一些解决方案,但似乎无法添加任何项目。最近我尝试过:
For Each folder As String In System.IO.Path.GetDirectoryName("D:\")
ListBox1.Items.Add(folder)
Next
The form was built using VB in VB Studio Express 2013. There were no errors when running the program.
该表单是在VB Studio Express 2013中使用VB构建的,运行程序没有报错。
If anyone can point me in the right direction, then please help!
如果有人能指出我正确的方向,那么请帮忙!
回答by Steve
If you want to have a list of Directories you need to call Directory.GetDirectories(path), not Path.GetDirectoryName(path)that, in your case returns just null (passing the root directory of a drive)
如果你想要一个目录列表,你需要调用Directory.GetDirectories(path),而不是Path.GetDirectoryName(path),在你的情况下只返回 null (传递驱动器的根目录)
For Each folder As String In System.IO.Directory.GetDirectories("D:\")
ListBox1.Items.Add(folder)
Next
if you want to show only the folder name and not the full path, just use
如果您只想显示文件夹名称而不显示完整路径,只需使用
For Each folder As String In System.IO.Directory.GetDirectories("D:\")
ListBox1.Items.Add(Path.GetFileName(folder))
Next
Yes, I know, it seems wrong to ask for GetFileName over a folder name, but passing a full path to GetFileName returns just the folder without the path.
是的,我知道,通过文件夹名称请求 GetFileName 似乎是错误的,但是将完整路径传递给 GetFileName 只会返回没有路径的文件夹。
回答by Muhammad Jana
how to select folder name to preview picture
如何选择文件夹名称来预览图片
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each folder As String In System.IO.Directory.GetDirectories("C:\Program Files (x86)\KONAMI\Pro Evolution Soccer 2016\SFXPath\SweetFX")
ListBox1.Items.Add(Path.GetFileName(folder))
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedItem = "folder" Then PictureBox1.ImageLocation = "C:\Program Files (x86)\KONAMI\Pro Evolution Soccer 2016\SFXPath\SweetFX\HD Natural by pimplo\preview.jpg"
End Sub
End Class

