I've seen a few examples but none so far in C#, what is the best way to select a random file under a directory?
In this particular case I want to select a wallpaper from "C:\wallpapers" every 15 or so minutes.
Thanks.
Right-click the folder in the left-hand pane — not the right — and click the new “Select Random” option. RandomSelectionTool then selects something from the contents of that folder — either a file, or a folder — and the right-hand pane should be updated to display it.
Click the first file or folder you want to select. Hold down the Shift key, select the last file or folder, and then let go of the Shift key. Hold down the Ctrl key and click any other file(s) or folder(s) you would like to add to those already selected.
Windows method one Click on one of the files or folders you want to select. Hold down the control key (Ctrl). Click on the other files or folders that you want to select while holding the control key. Continue to hold down the control key until you select all the files you want.
Get all files in an array and then retrieve one randomly
var rand = new Random();
var files = Directory.GetFiles("c:\\wallpapers","*.jpg");
return files[rand.Next(files.Length)];
If you're doing this for wallpapers, you don't want to just select a file at random because it won't appear random to the user.
What if you pick the same one three times in a row? Or alternate between two?
That's "random," but users don't like it.
See this post about how to display random pictures in a way users will like.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With