vb.net 使用列表视图作为文件/文件夹资源管理器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32350299/
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
Using listview as file/ folders explorer
提问by ABANDOND ACOUNT
I'm new to using listview to display icons. I have a few questions remaining...
我是使用 listview 显示图标的新手。我还有几个问题……
I am currently using the following code as example. This code works fine, I have questions though
我目前正在使用以下代码作为示例。这段代码工作正常,但我有问题
- How do I get rid of the path name and only keep the file / folder name?
- How do I also list folders alongside my files?
- How can I click open these icons?
- 如何去掉路径名而只保留文件/文件夹名?
- 我还如何在我的文件旁边列出文件夹?
- 如何点击打开这些图标?
-
——
For Each File In System.IO.Directory.GetFiles("C:\")
Dim icons As Icon = Icon.ExtractAssociatedIcon(File)
ListView1.Items.Add(File.ToString, ImageList1.Images.Count - 1)
ImageList1.Images.Add(icons)
ListView1.Items.Add(File.ToString, ImageList1.Images.Count)
Next
回答by Chris
You may have a look into using treeview... hope that helps
您可能会考虑使用树视图...希望有所帮助
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim x As Integer
For x = 0 To My.Computer.FileSystem.Drives.Count - 1
If My.Computer.FileSystem.Drives(x).IsReady = True Then
TreeView1.Nodes.Add(My.Computer.FileSystem.Drives(x).Name, My.Computer.FileSystem.Drives(x).Name)
TreeView1.Nodes(My.Computer.FileSystem.Drives(x).Name).Tag = My.Computer.FileSystem.Drives(x).Name
For Each SubDirectory As String In My.Computer.FileSystem.GetDirectories(My.Computer.FileSystem.Drives(x).Name)
TreeView1.Nodes(x).Nodes.Add(SubDirectory, Mid(SubDirectory, 4))
TreeView1.Nodes(x).Nodes(SubDirectory).Tag = SubDirectory
Next
End If
Next
End Sub
回答by ??ssa P?ngj?rdenlarp
Assuming you want more than just Fileand Foldername in the LV (like Explorer), I'd use DirectoryInfo:
假设你需要的不仅仅是更多的File和Folder名字的LV(比如浏览器),我会使用DirectoryInfo:
Dim lvi As ListViewItem
Dim di As New DirectoryInfo("C:\Temp")
Dim myIcon As Icon
' ext/icon lookup
Dim exts As New List(Of String)
ImageList1.Images.Clear()
For Each fi As FileInfo In di.EnumerateFiles("*.*")
lvi = New ListViewItem
lvi.Text = fi.Name
lvi.SubItems.Add(Path.GetDirectoryName(fi.FullName))
lvi.SubItems.Add(((fi.Length / 1024)).ToString("0.00"))
lvi.SubItems.Add(fi.CreationTime.ToShortDateString)
If exts.Contains(fi.Extension) = False Then
myIcon = Icon.ExtractAssociatedIcon(fi.FullName)
ImageList1.Images.Add(fi.Extension, myIcon)
exts.Add(fi.Extension)
End If
lvi.ImageKey = fi.Extension
myLV.Items.Add(lvi)
Next
The code uses a List(of String)to keep track of which icons have been added so that you do not add the same image over and over for repeated file types. other wise, it displays the Name, Folder, Size, Date and icon.
该代码使用List(of String)来跟踪添加了哪些图标,这样您就不会为重复的文件类型一遍又一遍地添加相同的图像。否则,它会显示名称、文件夹、大小、日期和图标。
The code using Directorywould rely on System.io.Pathmore and Size and Date would not be available:
使用的代码Directory将依赖于System.io.Pathmore 并且 Size 和 Date 将不可用:
For Each s As String In Directory.EnumerateFiles("C:\Temp")
lvi = New ListViewItem
lvi.Text = Path.GetFileName(s)
lvi.SubItems.Add(Path.GetDirectoryName(s))
Dim fileExt = Path.GetExtension(s)
If exts.Contains(fileExt) = False Then
myIcon = Icon.ExtractAssociatedIcon(s)
ImageList1.Images.Add(fileExt, myIcon)
exts.Add(fileExt)
End If
lvi.ImageKey = fileExt
myLV.Items.Add(lvi)
Next
How can I click open these iconsthat is a different question. Keep in mind that the LV just contains strings (text) and you removed the path from the file. So you will have to glue them back together first to get the legal name (or store it in the LV). That said, the LV has click and double click events like most other controls.
How can I click open these icons这是一个不同的问题。请记住,LV 仅包含字符串(文本)并且您从文件中删除了路径。因此,您必须先将它们重新粘在一起以获得合法名称(或将其存储在 LV 中)。也就是说,LV 与大多数其他控件一样具有单击和双击事件。


