最近公司需要一個生成大量隨機碼用於數字銷售。代碼如下: [csharp] class Program { static void Main(string[] args) { StreamWriter swriter = new StreamWriter("1.txt", true); for (int i = 0; i < 1000000; i++) { swriter.Write(generateRandomString(20)); swriter.WriteLine(); Console.WriteLine("Number: {0}", i); } swriter.Flush(); swriter.Close(); } static Random random = new Random(); static string generateRandomString(int length) { var chars = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"; StringBuilder result = new StringBuilder(); for (int i = 0; i < length; i++) { int index = random.Next(chars.Length); result.Append(chars[index]); } return result.ToString(); } } 上面是簡單的一個console的程序。 可改寫成wpf程序。