0

我想创建一个简单的打印机管理器以在我们的终端服务器环境中使用。由于 GPO 的限制,我可以使用的内置功能受到限制。所以我决定尝试编写自己的简单 GUI 来做到这一点。

现在,打印机分布在一个文件夹中,并带有用于对它们进行分类的子文件夹。在每个文件夹中,打印服务器上的实际打印机都有 .lnk 文件。

我想要做的是根据在树视图上单击的项目,在列表视图中填充文件夹和打印机的树视图。

我已经设法搜索目录并搜索我单击的每个项目的文件。但我意识到,为什么不在表单启动期间使用集合或类似的东西呢?这样,它会更快。因为现在,每次单击树视图中的项目时都会有一点延迟。因为它每次都会扫描文件。

如何将其添加到集合中并改用它?

这是我当前的代码:

Public Sub populateTreeView(ByVal strPath As String)

        Dim di As New IO.DirectoryInfo(strPath)
        Dim diar1 As IO.DirectoryInfo() = di.GetDirectories()
        Dim dra As IO.DirectoryInfo

        For Each dra In diar1
            ImageList1.Images.Add(GetSmallIcon(dra.FullName))

            TreeView1.Nodes.Add("", dra.Name, nIndex)
            nIndex = nIndex + 1
        Next
    End Sub

    Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect
        ListView1.Clear()
        nIndex = 0

        Dim di As New IO.DirectoryInfo(strIniSettings & "\" & TreeView1.SelectedNode.Text)
        Dim diar1 As IO.FileInfo() = di.GetFiles()
        Dim dra As IO.FileInfo

        For Each dra In diar1
            Dim strName As String
            strName = Replace(dra.Name, ".lnk", "")
            ImageList2.Images.Add(GetLargeIcon(dra.FullName))

            ListView1.Items.Add("", strName, nIndex)
            nIndex = nIndex + 1
        Next
    End Sub

注意图像列表?我也得到了每个项目的图标。

4

1 回答 1

1

由于您的数据并不复杂,因此一个简单LookUp的集合可能适合您(或者只是一个普通的字典)。

只需查询打印机一次,并将其存储在成员变量中,或者仅使用 s 的Tag属性来TreeNode存储文件名。

在下面的示例中,我使用一个简单的 Linq 查询来创建一个LookUpwhereKey是目录名(您也可以只使用目录的完整路径),并且项目是文件名。

然后,您可以通过给定Key的(目录名称)查询集合,或使用该Tag属性。


LINQPad 示例:

Sub Main

    ' query printers once (just replace C:\test with your path)
    ' store the result in a member variable of your form
    Dim printer = new DirectoryInfo("C:\test").GetDirectories() _
                                              .SelectMany(Function(d) d.GetFiles()) _
                                              .ToLookup(Function(f) f.Directory.Name, Function(f) f.Name)

    ' Or, using a Dictionary
    ' Dim printer = new DirectoryInfo("C:\test").GetDirectories() _
    '                                           .ToDictionary(Function(d) d.Name, Function(d) d.GetFiles().Select(Function(f) f.Name).ToList())



    Dim l = new ListView() With {.Dock = DockStyle.Right}
    Dim t = new TreeView() With {.Dock = DockStyle.Left}                    
    AddHandler t.AfterSelect, Sub(s, e)
                                    ' This is your AfterSelect event handler
                                    ' The filenames are stored in the Tag of the TreeNode
                                    ' You could also use 'For Each p As String in printer(e.Node.Text)'
                                    l.Items.Clear()
                                    For Each p As String in e.Node.Tag
                                        Dim item = l.Items.Add(p.Replace(".lnk", ""))
                                        'TODO: Set Icon on item
                                    Next
                              End Sub

    ' Populate TreeView once
    For Each folder in printer
        Dim fNode = t.Nodes.Add(folder.Key)
        'TODO: Set Icon on fNode

        ' store the files in the Tag of the node.
        ' You don't have to, but it will make it easier
        fNode.Tag = folder
    Next

    ' Show test form            
    Dim w = new Form()
    w.Controls.Add(t)
    w.Controls.Add(l)
    w.Show()

End Sub
于 2012-10-04T08:39:26.333 回答