I made an application that first of all loads all folders in root (C:\) in Listbox1. When you click on one of these folders, all .TXT files in it's subdirectories have to be shown in Listbox2.
using System;
using System.Windows.Forms;
using System.IO;
namespace Week10 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
loadRootDirInListbox(@"C:\");
}
private void loadRootDirInListbox(string path) {
foreach(string item in Directory.GetDirectories(path)) {
listBox1.Items.Add(item);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
int clicked = listBox1.SelectedIndex;
string[] files = getTXTfilesInDir(listBox1.Items[clicked].ToString());
loadInListbox2(files);
}
private string[] getTXTfilesInDir(string path) {
return Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
}
private void loadInListbox2(string[] items) {
listBox2.Items.Clear();
foreach(string item in items) listBox2.Items.Add(item);
}
}
}
Except some folders give this exception when clicked upon. For example, when I click on C:\Users I will get the following exception:
"System.UnauthorizedAccessException: 'Access to the path C:\Users\All Users\Application Data is denied.'"
I tried running VS17 in Administrator mode but that didn't make a difference. Is there a way to skip these unauthorized folders such as 'Application Data'?