+92 332 4229 857 99ProjectIdeas@Gmail.com

Random String Generator (C#.net)


Random String Generator
The following code shows you how to generate a random string and creating the random folder of that string at the mentioned path.
randomString() function generates a random string and creates the folder of that string also.


Code
 
public void randomString()
{
   StringBuilder builder = new StringBuilder();
   Random rand = new Random();
   char ch;

Here, also getting random size of that string which is to be generated, basically it is a length of that string.
   int size = rand.Next(10,18);

    for (int i = 0; i < size; i++)
    {

Now, here converting every integer value that is generated by the random function to its corresponding ASCII value characters. Capital 'A' to small 'z'

        ch = Convert.ToChar(rand.Next(65, 122));

Now , appending each character and make a string.

        builder.Append(ch);
    }

Now , creates the folder of that random string at your mentioned drive path.
    Directory.CreateDirectory(@"D:\" + builder);

output of the program is (First Time Execution):
FLAiDbKvOj

output of the program is (Second Time Execution):
eDthvXecQcWxhrq

So you see, every time run this code it would generate a random string and also create a folder of that generated random string on your mentioned path. 

Related Article: 

Random Number Generator

0 comments: