1

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'?

  • You can add some code to first check permissions on the file or folder such as shown here: https://stackoverflow.com/questions/1410127/c-sharp-test-if-user-has-write-access-to-a-folder – user1934587390 Nov 01 '17 at 19:10
  • You could wrap it the directory or path portions in a try catch, and scrub for the error. If it catches the exception, break or return. But being proactive and checking for permissions as Chris suggested above would be a more viable, practical solution. – apollosoftware.org Nov 01 '17 at 19:11
  • What if I still want to get files from the folders that I do have permission to? The only subdirectory of "C:\Users\" that I don't have access to is 'Application Data'. –  Nov 01 '17 at 19:17
  • I also don't understand why I would need write acecss because all I need is the name of a file. I'm not going to modify it. –  Nov 01 '17 at 19:21
  • @DirkvanderBurgt The only way to not get the exception is to only look in folders where you have appropriate permissions. The only way to find out if you have appropriate permissions is to check every folder, regardless if you think you have access. The code doesn't know until it checks, or tries to do something that needs elevated permissions and then it throws an error. You can also put a try/catch around the method that looks for files and then ignore folders that throw the exception. – user1934587390 Nov 01 '17 at 19:23
  • Here is a more in-depth example of different approaches for things like this: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-iterate-through-a-directory-tree – user1934587390 Nov 01 '17 at 19:29
  • @Chris The problem is that I do have permission to the folder that I peform the method on, but not on one of the subdirectories. So if I follow your solution then I will need to change all the code completely because I have to check permission per subdirectory. I am now looking into the DirectoryInfo class and am hoping that this class acts differently. –  Nov 01 '17 at 19:33
  • @DirkvanderBurgt I understand the problem, but that's the way the particular way of accessing the file system you are using works. It is also addressed in the link provided above: "The weakness in this approach is that if any one of the subdirectories under the specified root causes a DirectoryNotFoundException or UnauthorizedAccessException, the whole method fails and returns no directories. The same is true when you use the GetFiles method. If you have to handle these exceptions on specific subfolders, you must manually walk the directory tree, as shown in the following examples." – user1934587390 Nov 01 '17 at 19:36
  • @Chris Thanks for explaining it to me. I will look at the guide to create a method manually. –  Nov 01 '17 at 19:43

0 Answers0