public  static  string  GetRandomString(  int  length  )  
{  
           Random  rd  =  new  Random();  
           byte[]  str  =  new  byte[  length  ];  
                                     
           int  i;  
           for(  i=0;i<length;i++  )  
           {  
                       int  a  =  0;  
                       //while  后的条件用来限定密码的字符集,本例中是限定为大小写字母  
                             while(  !((a>=48  &&  a<=57)    &brvbar;  &brvbar;  (a>=65  &&  a<=90)    &brvbar;  &brvbar;  (a>=97  &&  a<=122))  )  
                       {  
                                   a  =  rd.Next(48,125);  
                       }  
                       str[i]  =  (byte)a;  
           }  
                                     
return  new  string(UnicodeEncoding.ASCII.GetChars(str));  
}