如何获取随机文件夹的路径?

发布于 2024-08-27 06:09:25 字数 193 浏览 4 评论 0原文

就像从 c:\ (或无论主驱动器是什么)开始,然后随机选择路线?甚至不知道该怎么做。

public sealed static class FolderHelper
{
    public static string GetRandomFolder()
    {
        // do work
    }
}

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
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

£烟消云散 2024-09-03 06:09:25

我掷骰子并得出了这个答案:

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

或者您可以使用 Random.Next()。

I rolled a die and came up with this answer:

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

Or you could use Random.Next().

晚风撩人 2024-09-03 06:09:25

尝试获取目录中所有文件夹的列表,然后生成一个最多为文件夹数量的随机数,然后选择与随机数相关的文件夹。

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];

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];
莫相离 2024-09-03 06:09:25

首先,您需要从中选择一些内容,例如目录中的所有子目录,因此您需要指定该父目录。然后你只需获取目录并随机选择一个:

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)];
}

如果你要多次执行此操作,你应该考虑创建一个类,以便你可以读取文件夹并创建随机生成器并存储在类中当您创建类的实例,然后在方法中使用它们时。

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.

も让我眼熟你 2024-09-03 06:09:25

我使用此代码从给定根文件夹的子文件夹树中获取随机文件夹

private string GetRandomFolder(string root)
{
  var rnd = new Random();
  var path = root;
  var depth = rnd.Next(0, 7);

  for (int i = 0; i < depth; i++)
  {
    path = this.GetRandomFolder(path);
    if (path == "")
      break;
  }

  return output;
}
private string GetRandomSubFolder(string root)
{
  var di = new DirectoryInfo(root);
  var dirs = di.GetDirectories();
  var rnd = new Random();

  if (dirs.Length == 0)
    return "";

  return dirs[rnd.Next(0, dirs.Length - 1)].FullName;
}

I use this code to get a random folder from sub-folders tree of a given root folder

private string GetRandomFolder(string root)
{
  var rnd = new Random();
  var path = root;
  var depth = rnd.Next(0, 7);

  for (int i = 0; i < depth; i++)
  {
    path = this.GetRandomFolder(path);
    if (path == "")
      break;
  }

  return output;
}
private string GetRandomSubFolder(string root)
{
  var di = new DirectoryInfo(root);
  var dirs = di.GetDirectories();
  var rnd = new Random();

  if (dirs.Length == 0)
    return "";

  return dirs[rnd.Next(0, dirs.Length - 1)].FullName;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文