如何获得随机文件夹的路径? [英] How do I get the path of a random folder?

查看:179
本文介绍了如何获得随机文件夹的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像从c:\(或任何主驱动器)开始,然后随机走路线?

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


推荐答案

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

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

这篇关于如何获得随机文件夹的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆