1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Open a random file with C#?

Discussion in 'C, C++, C#' started by OrangeNRG, Oct 12, 2013.

  1. OrangeNRG

    OrangeNRG Jr. VIP Jr. VIP Premium Member

    Joined:
    Dec 10, 2012
    Messages:
    381
    Likes Received:
    245
    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
     
  2. rootjazz

    rootjazz Jr. VIP Jr. VIP

    Joined:
    Dec 21, 2012
    Messages:
    614
    Likes Received:
    313
    Occupation:
    Developer
    Location:
    UK
    Home Page:
    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.
     
  3. d4dennis

    d4dennis Newbie

    Joined:
    Aug 22, 2011
    Messages:
    33
    Likes Received:
    4
    Occupation:
    Software Developer
    Location:
    Melbourne, Australia
    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!
     
  4. sm754

    sm754 Registered Member

    Joined:
    Mar 21, 2012
    Messages:
    93
    Likes Received:
    38
    Occupation:
    Farmer
    Location:
    Azerbaijan
    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: Oct 12, 2013
  5. OrangeNRG

    OrangeNRG Jr. VIP Jr. VIP Premium Member

    Joined:
    Dec 10, 2012
    Messages:
    381
    Likes Received:
    245
    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.

    [​IMG]
     
  6. qrazy

    qrazy Senior Member

    Joined:
    Mar 19, 2012
    Messages:
    1,111
    Likes Received:
    1,712
    Location:
    Banana Republic
    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'

     
  7. Jannes

    Jannes Newbie

    Joined:
    Jan 9, 2010
    Messages:
    35
    Likes Received:
    15
    Maybe you need to read and try to understand what you are doing.
    http://msdn.microsoft.com/en-us/library/67ef8sbd.aspx
     
    • Thanks Thanks x 1
  8. OrangeNRG

    OrangeNRG Jr. VIP Jr. VIP Premium Member

    Joined:
    Dec 10, 2012
    Messages:
    381
    Likes Received:
    245
    Thanks for the help. :) PM me for an article
     
  9. Chris22

    Chris22 Regular Member

    Joined:
    Sep 29, 2010
    Messages:
    400
    Likes Received:
    1,059
    You could use Linq

    Code:
    string file = Directory.GetFiles(Directory.GetCurrentDirectory()).OrderBy(x => Guid.NewGuid()).First()
     
  10. always eval

    always eval Newbie

    Joined:
    Oct 23, 2013
    Messages:
    6
    Likes Received:
    0
    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)
     
  11. neutralhatter

    neutralhatter Jr. VIP Jr. VIP Premium Member

    Joined:
    Jun 23, 2010
    Messages:
    430
    Likes Received:
    330
    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)


     
    Last edited: Dec 2, 2013
  12. always eval

    always eval Newbie

    Joined:
    Oct 23, 2013
    Messages:
    6
    Likes Received:
    0
    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.
     
  13. seeplusplus

    seeplusplus Power Member

    Joined:
    Aug 18, 2008
    Messages:
    511
    Likes Received:
    163
    Has this been solved yet? Like the first post, form an array any select any element by random().

    Index != type and all that.
     
  14. anaman123456

    anaman123456 Newbie

    Joined:
    Dec 31, 2013
    Messages:
    7
    Likes Received:
    0
    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();

    }