Random file name

Discussion in 'Mixed Languages' started by Alphawaves, Dec 5, 2012.

  1. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,886
    20,280
    180
    #1 Alphawaves, Dec 5, 2012
    Last edited by a moderator: Apr 20, 2017
    Ok i made a random file namer a long time ago, here it is if it is useful for anyone:
    Code:
     private string filename;
    
     private string RandomString(int size, bool upperCase)
            {
                StringBuilder build = new StringBuilder();
                Random random = new Random();
                char ch;
                for (int i = 0; i < size; i++)
                {
                    ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                    build.Append(ch);
                }
                if (upperCase) return build.ToString().ToUpper();
                return build.ToString();
            }
    Usage:
    NOTE: You can also add bytes/writebytes to this
    Code:
     filename = RandomString(5, true);// where 5 = file string length
     File.Create(@"bla\bla\bla\" + filename);
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,106
    120
    Awesome! It's really useful for apps that needs of random string generations or other stuff like that.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...