I want to create a simple printer manager to use in our Terminal server environment. Because of GPO restrictions, there are limits of what built-in functionality I can use. So I decided to try to write my own simple GUI to do that.
Now, the printers are distributed in a folder, with subfolders to categorize them. In each folder there are .lnk files to the actual printer on the printserver.
What I want to do is to populate a treeview with the folders, and the printers in a listview, based on which item is clicked on the treeview.
I’ve already managed to search for directories and to search the files for each item I’ve clicked. But I realized, why not use a collection or similar to do this during the startup of the form? That way, it’ll be faster. Because right now, there’s a small delay each time I click an item in the treeview. Because it scans for files each time.
How can I add the same to a collection and use that instead?
Here’s my current code:
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
Notice the Imagelists? I also get the Icon for each item as well.
Since your data is not complex, a simple
LookUpmay be the right collection for you (or just a plain Dictionary).Just query the printers once, and store it in a member variable, or just use the
Tagproperty of theTreeNodes so store the file names.In the example below, I’m using a simple Linq query to create a
LookUpwhere theKeyis the directory name (you could also just use full path to the directory), and the items are the file names.You could then either query the collection by a given
Key(the directory name), or use theTagproperty.LINQPad example: