Reputation: 590
I've found this simple File Explorer for WPF on sharpcorner.
namespace WindowsExplorer
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
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";
}
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.Type = DR.GetType().Name;
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);
}
}
void CTVI_Expanded(object sender, RoutedEventArgs e)
{
MyTreeViewItem TVI = (MyTreeViewItem)sender;
foreach (MyTreeViewItem CTVI in TVI.Items)
{
if (CTVI.Type == "DirectoryInfo")
{
DirectoryInfo DIR = new DirectoryInfo(CTVI.Path);
foreach (DirectoryInfo CDIR in DIR.GetDirectories())
{
MyTreeViewItem CTVI1 = new MyTreeViewItem();
CTVI1.Expanded += new RoutedEventHandler(CTVI_Expanded);
CTVI1.Header = CDIR.Name;
CTVI1.Path = CDIR.FullName;
CTVI1.Type = CDIR.GetType().Name;
if (CTVI.Items.Contains(CTVI1.Header) == false)
CTVI.Items.Add(CTVI1);
}
}
}
e.Handled = true;
}
private void tvwDirectory_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
if (e.NewValue != null)
{
this.ltbExplorer.Items.Clear();
MyTreeViewItem TVI = (MyTreeViewItem)e.NewValue;
DirectoryInfo DIR = new DirectoryInfo(TVI.Path);
foreach (FileInfo FL in DIR.GetFiles())
{
ltbExplorer.Items.Add(FL.Name);
}
}
e.Handled = true;
}
}
}
namespace WindowsExplorer
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
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";
}
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.Type = DR.GetType().Name;
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);
}
}
void CTVI_Expanded(object sender, RoutedEventArgs e)
{
MyTreeViewItem TVI = (MyTreeViewItem)sender;
foreach (MyTreeViewItem CTVI in TVI.Items)
{
if (CTVI.Type == "DirectoryInfo")
{
DirectoryInfo DIR = new DirectoryInfo(CTVI.Path);
foreach (DirectoryInfo CDIR in DIR.GetDirectories())
{
MyTreeViewItem CTVI1 = new MyTreeViewItem();
CTVI1.Expanded += new RoutedEventHandler(CTVI_Expanded);
CTVI1.Header = CDIR.Name;
CTVI1.Path = CDIR.FullName;
CTVI1.Type = CDIR.GetType().Name;
if (CTVI.Items.Contains(CTVI1.Header) == false)
CTVI.Items.Add(CTVI1);
}
}
}
e.Handled = true;
}
private void tvwDirectory_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
if (e.NewValue != null)
{
this.ltbExplorer.Items.Clear();
MyTreeViewItem TVI = (MyTreeViewItem)e.NewValue;
DirectoryInfo DIR = new DirectoryInfo(TVI.Path);
foreach (FileInfo FL in DIR.GetFiles())
{
//MyTreeViewItem CTVI2 = new MyTreeViewItem();
//CTVI2.Expanded += new RoutedEventHandler(CTVI_Expanded);
//CTVI2.Header = FL.Name;
//CTVI2.Path = FL.FullName;
//CTVI2.Type = FL.GetType().Name;
ltbExplorer.Items.Add(FL.Name);
}
}
e.Handled = true;
}
}
}
.
The program works. But if I click on drive C: for example, I get this error:
System.UnauthorizedAccessException was unhandled
.
It concerns the first foreach:
foreach (DirectoryInfo CDIR in DR.GetDirectories())
.
If I start the program as administrator, it works. But can I avoid/solve this problem without being administrator?
Upvotes: 0
Views: 748
Reputation: 10889
Your problem occurs earlier: foreach (DirectoryInfo DR in DIR.GetDirectories())
C:\
contains folders which are only accessible with admin rights.
You try to get infos on such folders and the program throws an exception because you are not allowed to get that kind of information.
A possible workaround looks like this:
try
{
foreach (DirectoryInfo DR in DIR.GetDirectories())
{
// stuff
}
}
catch(exception ex)
{
//do something with the failure. maybe ignore it
}
Upvotes: 2