之前找了一個C#的驗證碼參考,但有一句用VB.NET卻找不出該怎樣寫,以致好長一段時間我的驗證碼都是重復的,今天在小鋪網友的耐心指點下,用 Dim aa As Integer = CType(DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer)作了Random的種子,但是設中斷情況下執行才正常,若沒設中斷則會出現System.StackOverflowException' 的未處理例外狀況發生於 mscorlib.dll的錯誤
於是修正代碼如下,總算跑出自己尚算滿意的結果來
Dim allChar As String = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z"
Dim allCharArray() As String = allChar.Split(",")
Dim randomCode As String = ""
Dim temp As Integer = -1
Dim rand As Random = New Random
Dim i As Integer = 0
Do While (i < codeCount)
If (temp <> -1) Then
'rand = New Random(i * temp)若沒有下面僅這裏會時常重復的
Dim aa As Integer = CType(DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer)'用VB.NET寫法,去掉了與I和TEMP的相乘
rand = New Random(aa)
End If
Dim t As Integer = rand.Next(61) + 1'+1是改寫,61表示字符的總個數,索引值從0開始
If t > allCharArray.Length - 1 Then t = allCharArray.Length - 1'改寫
If temp = t Then'抑止驗證碼的連續重復
' Return createrandomcode(codecount)出現mscorlib.dll的死胡同
i -= 1'改寫
randomCode = Microsoft.VisualBasic.Left(randomCode, i)'改寫
End If
temp = t
randomCode = randomCode + allCharArray(t)
i += 1
Loop
Return randomCode
盡信書則不如無書,這是我最想說的一句話,不要讓信條束縛了自己的思想喔^__^