y2k y2k - 1 year ago 117
C# Question

How do I get the path of a random folder?

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

Answer Source

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;
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];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download