tags:

views:

86

answers:

3

Like start at c:\ (or whatever the main drive is) and then randomly take routes? Not even sure how to do that.

public sealed static class FolderHelper
{
    public static string GetRandomFolder()
    {
        // do work
    }
}
+3  A: 

Try getting a list of all the folders in the directory, then generate a random number up to the number of folders, then choose the folder that relates to your random number.

System.IO.DirectoryInfo[] subDirs = null;
System.IO.DirectoryInfo root;
// assign the value of your root here
subDirs = root.GetDirectories();
Random random = new Random();
int directory = Random.Next(subDirs.length());
System.IO.DirectoryInfo randomDirectory = subDirs[directory];
Bryan Denny
+5  A: 

I rolled a die and came up with this answer:

  public static string GetRandomFolder()
    {
        return "4";
    }

Or you could use Random.Next().

Hans Passant
+1 for `Random.Next`. -1 for xkcd reference.
Aaronaught
Somebody thought of this before??
Hans Passant
http://xkcd.com/221/
Guffa
+1  A: 

First of all you need something to pick from, for example all subdirectories in a directory, so then you need to specify that parent directory. Then you just get the directories and pick one by random:

public static string GetRandomFolder() {
  string parentFolder = @"c:\some\folder\somewhere";
  string[] folders = Directory.GetDirectories(parentFolder);
  Random rnd = new Random();
  return folders[rnd.Next(folders.Length)];
}

If you are going to do this more than once, you should consider making a class of it, so that you can read in the folders and create the random generator and store in the class when you create an instance of the class, and then just use them in the method.

Guffa