cmbDrives.DataSource = Environment.GetLogicalDrives();
I have this code to show all drives in combobox, and want to show All Drive's related data in treeview on comboboxselectionchanged event.
How can I do that?
cmbDrives.DataSource = Environment.GetLogicalDrives();
I have this code to show all drives in combobox, and want to show All Drive's related data in treeview on comboboxselectionchanged event.
How can I do that?
Copyright © 2021 Jogjafile Inc.
The following code achieves what you want by leveraging a backgroundworker to make sure the UI stays responsive while progress is made during the folder traversal.
First add a ComboBox, a Treeview and a BackgroundWorker to your design surface. Make sure to add the events used in the following code from the Properties window for each control using the eventview. Make sure to set the property
WorkerReportsProgressto true for the BackgroundWorker.First the ComboBox SelectionIndexChanged event:
We disable our combobox to prevent selecting a new value while we are handling a drive traversal. Then we start our BackgroundWorker passing it the SelectedItem which we assume is a valid drive.
Background worker
The DoWork event takes an eventargs that has an Argument property that contain the value we provided with calling RunWorkerAsync. As we our now on a non-UI thread we need some trickery to update the UI. The background worker can do that with its ReportProgress method. It takes an integer, normally used for how far we are and an optional userstate. We use those two to indicate which state we are in and provide a TreeNode that needs be added or updated on our TreeView. It then call
GetDirectoryNodesto do the directory traversal.ProgressChanged is called every time we have a TreeNode (or Tuple of TreeNodes) so we can add nodes to the TreeView or one of its nodes while we are at the UI thread. The
ProgressPercentageis not used to show how far we are but to distinguish between the initial state and the traversal state.Once we are done (that is every folder is traversed) we enable the combobox again so we can select a new drive.
Drive folder traversal
To traverse a directory tree I use a recursive function (loosely inspired on the answer from Chris Ballard that calls the BackgroundWorkers
ReportProgressmethod once it has TreeNode that is ready to be added to the TreeView. Notice how I use a Tuple as a state object that holds the Parent and the Child to be added but that specific action is left for the ProgressChanged event on UI-Thread.