0

I have the following code that should recursively look for my file in the app's main directory, including subfolders:

public static List<string> FindFileInDirectory(string filename)
{
    List<string> output = new List<string>();
    try
    {
        foreach(string d in Directory.GetDirectories(AppDomain.CurrentDomain.BaseDirectory))
        {
            foreach (string f in Directory.GetFiles(d, filename))
            {
                output.Add(f);
            }
            FindFileInDirectory(d);
        }
    }catch(System.Exception anyEx)
    {
        Debug.Print(anyEx.Message);
    }

    return output;
}

Could someone skilled guide me to get around this?

Error:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: The index pointed outside of the permitted range. The index can't be negative and has to be less than the collection's size.

Where it occurs:

I have a Mongoose http server that's just an exe and I wish to find it within my directory to start it (if not running already). I include the constructor of my class, there are only some fields for the ease of use in it. The problem is with the search that was found here. The code for that is:

public MongooseHttpServer()
{
    process = Process.GetProcessesByName("mongoose-free-6.5.exe");
    if (process.Length == 0)
    {
        //http server not running, have to start
        Debug.Print("not running, but starting now...");
        process = new Process[1];
        //in this case the first is enough.
        mongooseLocation = DirectorySearcher.FindFileInDirectory("mongoose-free-6.5.exe")[0]; //error here.
        process[0] = Process.Start(mongooseLocation ?? Directory.GetCurrentDirectory());               
    }
    else
    {
        Debug.Print("already running");
    }
}
Community
  • 1
  • 1
agiro
  • 2,018
  • 2
  • 30
  • 62
  • 1
    Start with *which line* throws the exception... – Matt Burland Mar 16 '17 at 18:02
  • Sorry, correcting right away. – agiro Mar 16 '17 at 18:02
  • 1
    So the obvious answer is that `FindFileInDirectory` is returning an empty array. – Matt Burland Mar 16 '17 at 18:06
  • 1
    See [here](http://stackoverflow.com/a/2700080/1250301) – Matt Burland Mar 16 '17 at 18:08
  • 1
    Your code declares the output list inside the FindFileInDirectory. Each recursive call recreates this list but at the end the only list returned is the one of the first call and it is probable that there is no file in that directory – Steve Mar 16 '17 at 18:08
  • @Steve Thanks, I was so eager to copy-paste that I didn't even give a think to that. So declaring outside wouldn't hurt? – agiro Mar 16 '17 at 18:10
  • 2
    No, but @MattBurland has pointed you to a better solution. Use the overload of GetFiles that scans all the subfolders. You don't need this code at all and always check the bound of the returned list. – Steve Mar 16 '17 at 18:11
  • Yup, got it now, no need to reinvent the wheel. @MattBurland Could you convert that to an answer so I can accept? – agiro Mar 16 '17 at 18:14
  • 2
    @agiro: No need. The question is ultimately a dupe. No need to reinvent the question... – Matt Burland Mar 16 '17 at 18:15

0 Answers0