G'day all,

When i'm using the search function (Below) I get a Unauthorized Access Exception on certain folders, which isn't a problem, however after this Exception i'm not getting any results. I have tried searching around but I couldn't make sense of alot of stuff that I found. I haven't much experience in exception handling so any insight would be good. I've tried throwing the exeption and using "continue" but I don't really understand how to do that. Thanks!

try
            {
                // Loop through each ticked drive
                foreach (string chkDrive in CkBox_Drives.CheckedItems)
                {
                        foreach (string files in Directory.GetFiles(chkDrive, "*.exe", SearchOption.AllDirectories))
                        {
                            CkBox_Found.Items.Add(files);
                        }
                }
            }
          catch (System.UnauthorizedAccessException excpt)
            {
               LBox_Errors.Items.Add((excpt.Message));
           }

Directory.GetFiles() wont cut it for what you're trying to do:

private void button2_Click(object sender, EventArgs e)
    {
      string[] files = GetFiles(@"C:\dotnetwin\", "*.cs");
      MessageBox.Show("Files found: " + files.Length.ToString());
      //System.Diagnostics.Debugger.Break();
    }
    public static string[] GetFiles(string searchPath, string pattern)
    {
      return GetFiles(searchPath, pattern, default(List<string>));
    }
    private static string[] GetFiles(string searchPath, string pattern, List<string> files)
    {
      if (files == null)
        files = new List<string>();
      try
      {
        files.AddRange(Directory.GetFiles(searchPath, pattern));
      }
      catch { } //throw everything away
      
      string[] dirs = null;

      try
      {
        dirs = Directory.GetDirectories(searchPath);
      }
      catch { }
      if (dirs != null)
      {
        foreach (string dir in dirs)
          GetFiles(dir, pattern, files);
      }
      return files.ToArray();
    }

Thanks for the reply mate. I don't really know why it wouldn't be good enough to do what I want. Could you perhaps explain it for me?. I'll go over your code as well, thanks for that!

Because you want the iterations to continue on failure and the method you're calling is an all-or-nothing approach. If you want something in between you need to roll your own solution.

Have you had any progress with your application?

Because you want the iterations to continue on failure and the method you're calling is an all-or-nothing approach. If you want something in between you need to roll your own solution.

Have you had any progress with your application?

Oh ok, well that actually makes sense. I've implimented the code you provided and it works really well. Going over it, I can't fully grasp what its doing but thats ok, I have time to figure it out. I have another question, but i'll put it in another post - its to do with String parsing. The progress on my app is like a snow ball really. Very slow at the moment, but its getting faster :D.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.