如何获取随机文件夹的路径?
就像从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我掷骰子并得出了这个答案:
或者您可以使用 Random.Next()。
I rolled a die and came up with this answer:
Or you could use Random.Next().
尝试获取目录中所有文件夹的列表,然后生成一个最多为文件夹数量的随机数,然后选择与随机数相关的文件夹。
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.
首先,您需要从中选择一些内容,例如目录中的所有子目录,因此您需要指定该父目录。然后你只需获取目录并随机选择一个:
如果你要多次执行此操作,你应该考虑创建一个类,以便你可以读取文件夹并创建随机生成器并存储在类中当您创建类的实例,然后在方法中使用它们时。
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:
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.
我使用此代码从给定根文件夹的子文件夹树中获取随机文件夹
I use this code to get a random folder from sub-folders tree of a given root folder