private string CreatePassword()
{
string passwd = string.Empty;
Random rnd = new Random();
for (int i = 1; i <= 8; i++)
{
int DigiStr = rnd.Next(0, 3);
//generate a number between 0-9
if (DigiStr == 0)
passwd += (char)rnd.Next(48, 58);
if (DigiStr == 1)
passwd += (char)rnd.Next(65, 91);
if (DigiStr == 2)
passwd += (char)rnd.Next(97, 123);
}
System.Threading.Thread.Sleep(5);
return passwd;
}



فراخواني:

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(CreatePassword().ToString());
}