Open a random file with C#?

OrangeNRG

Regular Member
Joined
Dec 10, 2012
Messages
411
Reaction score
262
I'm trying to learn how to use C# to open a random file within a folder on my hard drive. The purpose of this is to run the code midway through a macro to automate a process. The files will usually be videos but could be any type. I've searched but if anybody could point me to the right direction, please do.

Code:
http://stackoverflow.c0m/questions/742887/select-random-file-from-directory
This is the best I've found but that looks like he's doing it on a website and not his computer
 
From the top of my head I would do

Code:
1) get an array of files in a folder, hint use DirectoryInfo.GetFiles(..) GetFiles will take a file type filter I believe
2) Select an index at random, hint use Random

If you get stuck with the code, post up your questions.
 
Hi There,

The example showing on the website is exactly what you need.

What do you mean "he is doing it on a website not his computer" ?

Example:

Code:
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]            
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]// get an array of all files. [SIZE=2][FONT=Consolas]Server.MapPath([FONT=Consolas][COLOR=#a31515][FONT=Consolas][COLOR=#a31515][FONT=Consolas][COLOR=#a31515]"~/app_data/video/"[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Consolas][FONT=Consolas])[/FONT][/FONT][/FONT][/SIZE] convert relative path to computer path e.g. c:\inetpub\wwwroot\app_data\video\[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]            
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][] allfiles = System.IO.[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]Directory[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].GetFiles(Server.MapPath([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"~/app_data/video/"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]));[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]
            
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]// use random index to retrieve file[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]            
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] filename = allfiles[[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]Random[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]().Next(0, allfiles.Length-1)];[/SIZE][/FONT][/SIZE][/FONT]

Feel free to show what you have if youstill having problem.

Good luck!
 
Code:
[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] filename = allfiles[[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]Random[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]().Next(0, allfiles.Length-1)];[/SIZE][/FONT][/SIZE][/FONT]
This part needs to be changed; if you make a new Random every time you call the function, you may end up with the same value multiple times, since it's seeded from the system clock. I usually just keep a static, global Random object with threadlocks. Other than that, the code looks fine.
edit: if it's in a macro, you can probably disregard this. This is a problem for functions which can be called multiple times in one second
 
Last edited:
Thank you guys. I'm still stuck on a couple errors. I looked them up and no luck. I will hand write a 500 word article for the person who can help me to get this to work properly.

bf4e47aadc210abedbb745c58714bb49.png
 
You can just ignore Server.Mappath and pass in the directory as a string like GetFiles("c:\\something\\")

And for the second error leave a space between 'new' and 'Random'

Thank you guys. I'm still stuck on a couple errors. I looked them up and no luck. I will hand write a 500 word article for the person who can help me to get this to work properly.

bf4e47aadc210abedbb745c58714bb49.png
 
Maybe you need to read and try to understand what you are doing.
http://msdn.microsoft.com/en-us/library/67ef8sbd.aspx
 
You can just ignore Server.Mappath and pass in the directory as a string like GetFiles("c:\\something\\")

And for the second error leave a space between 'new' and 'Random'

Thanks for the help. :) PM me for an article
 
You could use Linq

Code:
string file = Directory.GetFiles(Directory.GetCurrentDirectory()).OrderBy(x => Guid.NewGuid()).First()
 
I second the need the for a global static random. Without it, if a function is called fast enough it can produce the same results.

Also when using Random.Next() and Array.Length there is no need to put -1. You are giving the inclusive lower and exclusive upper numbers. (Random.Next() will never hit the top number)

If you have 10 objects in your array there indexes are 0-9 meaning Array.Length = 10 which is fine. When you minus one the last object in your array is never being used.

(I tried posting the Random.Next() msdn reference,but I can't post links yet)
 
what are you talking about, op doesn't need a global static random. and if you do, you can just create a static class for it.

(though anyone creating a web app should never do this, this can only be stable in client side applications)


I second the need the for a global static random. Without it, if a function is called fast enough it can produce the same results.

Also when using Random.Next() and Array.Length there is no need to put -1. You are giving the inclusive lower and exclusive upper numbers. (Random.Next() will never hit the top number)

If you have 10 objects in your array there indexes are 0-9 meaning Array.Length = 10 which is fine. When you minus one the last object in your array is never being used.

(I tried posting the Random.Next() msdn reference,but I can't post links yet)
 
Last edited:
what are you talking about, op doesn't need a global static random. and if you do, you can just create a static class for it.

(though anyone creating a web app should never do this, this can only be stable in client side applications)

In this particular case you are correct, it is not required.

Was sort of just agreeing with some insight shared above that creating a new Random object each time a function is called can be problematic if the function is called fast enough.
 
Has this been solved yet? Like the first post, form an array any select any element by random().

Index != type and all that.
 
private string getrandomfile(string path)
{
ArrayList al = new ArrayList();
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] rgFiles = di.GetFiles("*.*");
foreach (FileInfo fi in rgFiles)
{
al.Add(fi.FullName);
}

Random r = new Random();
int x = r.Next(0,al.Count);

return al[x].ToString();

}
 
Back
Top