Introduction
Its easy to create a woindows explorer in WPF Useing C#. This article defines how to create windows explorer in WPF using C#, to create window explorer follow the bellow steps.
Take on combo box to your in to your windows and set the name property to cmbDirve, in the load event of windows bind the drives available in the system with combobox . see the bellow code
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.cmbDrive.ItemsSource = DriveInfo.GetDrives().Where(dr => dr.IsReady == true).ToList();
this.cmbDrive.DisplayMemberPath = "Name";
this.cmbDrive.SelectedValuePath = "Name";
}
Note:- the code 'this.cmbDrive.ItemsSource = DriveInfo.GetDrives().Where(dr => dr.IsReady == true).ToList();' Lists the Drives that is ready in the system.
Take a Tree view controls in to your project and set the name to tvDirectories. Create a custom Tree View Item buy creating a class that inherits from Tree View Item class.
Declare two string properties having name Path and Type that will store Name and path of directory.
Again take a List Box Control to lists contained file of current directory.
In the Select Index Changed event of Combo box clear the items of combo Box and create the object of Directory Info class. In the constructor of Directory info class pass the pass the selected value of combo box as string.
Using while loop retrieve the directories of current directory and pass to the custom tree view item and to the tree view
Add the files of current directory in to the items of List box.
Again retrieve the list of child directory,
pass it to the custom tree view item and add it into the items of current tree
view item.
Above step 5, 6 and 7 will list the Directories, child directories and files of
parent directory into the tree view. See the bellow code
private void cmbDrive_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.tvwDirectory.Items.Clear();
DirectoryInfo DIR = new DirectoryInfo(this.cmbDrive.SelectedValue.ToString());
foreach (DirectoryInfo DR in DIR.GetDirectories())
{
MyTreeViewItem TVI = new MyTreeViewItem();
TVI.Header = DR.Name;
TVI.Path = DR.FullName;
TVI.Expanded += new RoutedEventHandler(CTVI_Expanded);
if (!DR.Attributes.ToString().Contains("Hidden"))
{
foreach (DirectoryInfo CDIR in DR.GetDirectories())
{
MyTreeViewItem CTVI = new MyTreeViewItem();
CTVI.Expanded += new RoutedEventHandler(CTVI_Expanded);
CTVI.Header = CDIR.Name;
CTVI.Path = CDIR.FullName;
CTVI.Type = CDIR.GetType().Name;
TVI.Items.Add(CTVI);
}
this.tvwDirectory.Items.Add(TVI);
}
}
foreach (FileInfo FL in DIR.GetFiles())
{
this.ltbExplorer.Items.Add(FL.Name);
}
John Mark PadillaPosted Nov 3, 2019, 3:53 PM
Can you create it using Windows Forms?