0
List<string> alltypes = new List<string>();
try
{
    alltypes = Directory.GetFiles(textBox3.Text, "*.*", SearchOption.AllDirectories).ToList();
}
catch (UnauthorizedAccessException)
{

}
if (alltypes.Count > 0)
{
    if (comboBox1.Items.Count > 0)
        comboBox1.Items.Clear();
    comboBox1.Items.Add("Possible Extensions");
    foreach (string ext in alltypes)
    {
        if (!comboBox1.Items.Contains(Path.GetExtension(ext)))
            comboBox1.Items.Add(Path.GetExtension(ext));
    }    
}

When it gets to the catch it just stops, it never continues getting the files so the List alltypes is empty. For example in this case in textBox3.Text the value is c:\.

GSerg
  • 76,472
  • 17
  • 159
  • 346
מני מנחם
  • 235
  • 3
  • 12
  • What about checking for null / nothing (alltypes == null)? – Anthony Horne Jun 24 '16 at 07:44
  • 2
    @AnthonyHorne His catch is defined by `UnauthorihzedAccessException` so I guess his question is basicly about how to avoid not having the rights to access a folder. – C4d Jun 24 '16 at 07:45

1 Answers1

0

It depends on the rights needed to access the folder. There are too many options. For example it could be set by an active directory. Output your exception in the catch block an check the details.

But in general it can be broken down to: How to request administrator permissions when the program starts?

Community
  • 1
  • 1
C4d
  • 3,183
  • 4
  • 29
  • 50