utility.vb

来自「C#语言制作asp.net网上商店的」· VB 代码 · 共 741 行 · 第 1/2 页

VB
741
字号
            Loop
        End Sub


        Public Shared Function GetUserName() As String

            Dim sUserName As String = ""
            If HttpContext.Current.User.Identity.IsAuthenticated Then
                sUserName = HttpContext.Current.User.Identity.Name
            Else

                'we'll tag them with an anon userName until they register
                If Not HttpContext.Current.Request.Cookies("shopperID") Is Nothing Then
                    sUserName = HttpContext.Current.Request.Cookies("shopperID").Value
                Else

                    'if we have never seen them, return the current anonymous ID for the user
                    sUserName = HttpContext.Current.Profile.UserName
                End If
            End If
            HttpContext.Current.Response.Cookies("shopperID").Value = sUserName
            HttpContext.Current.Response.Cookies("shopperID").Expires = DateTime.Today.AddDays(365)
            Return sUserName
        End Function



#Region "Formatting bits"
#Region "IsNullOrEmpty"

        Public Shared Function IsNullOrEmpty(ByVal text As String) As Boolean
            If text Is Nothing OrElse (Not text Is Nothing AndAlso text.Length = 0) Then
                Return True
            End If

            Return False
        End Function

#End Region

#Region "CheckStringLength"

        Public Shared Function CheckStringLength(ByVal stringToCheck As String, ByVal maxLength As Integer) As String
            Dim checkedString As String = Nothing

            If stringToCheck.Length <= maxLength Then
                Return stringToCheck
            End If

            ' If the string to check is longer than maxLength 
            ' and has no whitespace we need to trim it down.
            If (stringToCheck.Length > maxLength) AndAlso (stringToCheck.IndexOf(" ") = -1) Then
                checkedString = stringToCheck.Substring(0, maxLength) & "..."
            ElseIf stringToCheck.Length > 0 Then
                Dim words As String()
                Dim expectedWhitespace As Integer = CInt(stringToCheck.Length / 8)

                ' How much whitespace is there?
                words = stringToCheck.Split(" "c)

                checkedString = stringToCheck.Substring(0, maxLength) & "..."
            Else
                checkedString = stringToCheck
            End If

            Return checkedString
        End Function

#End Region

#Region "FormatDate"

        Public Shared Function FormatDate(ByVal theDate As DateTime) As String
            Return FormatDate(theDate, False, Nothing)
        End Function

        Public Shared Function FormatDate(ByVal theDate As DateTime, ByVal showTime As Boolean) As String
            Return FormatDate(theDate, showTime, Nothing)
        End Function

        Public Shared Function FormatDate(ByVal theDate As DateTime, ByVal showTime As Boolean, ByVal pattern As String) As String
            Dim defaultDatePattern As String = "MMMM d, yyyy"
            Dim defaultTimePattern As String = "hh:mm tt"

            If pattern Is Nothing Then
                If showTime Then
                    pattern = defaultDatePattern & " " & defaultTimePattern
                Else
                    pattern = defaultDatePattern
                End If
            End If

            Return theDate.ToString(pattern)
        End Function

#End Region

#Region "UserIsAuthenticated"

        Public Shared Function UserIsAuthenticated() As Boolean
            Dim context As HttpContext = HttpContext.Current

            If Not context.User Is Nothing AndAlso Not context.User.Identity Is Nothing AndAlso (Not Utility.IsNullOrEmpty(context.User.Identity.Name)) Then
                Return True
            End If

            Return False
        End Function

#End Region

#Region "StripHTML"

        Public Shared Function StripHTML(ByVal htmlString As String) As String
            Return StripHTML(htmlString, "", True)
        End Function

        Public Shared Function StripHTML(ByVal htmlString As String, ByVal htmlPlaceHolder As String) As String
            Return StripHTML(htmlString, htmlPlaceHolder, True)
        End Function

        Public Shared Function StripHTML(ByVal htmlString As String, ByVal htmlPlaceHolder As String, ByVal stripExcessSpaces As Boolean) As String
            Dim pattern As String = "<(.|\n)*?>"
            Dim sOut As String = System.Text.RegularExpressions.Regex.Replace(htmlString, pattern, htmlPlaceHolder)
            sOut = sOut.Replace("&nbsp;", "")
            sOut = sOut.Replace("&amp;", "&")

            If stripExcessSpaces Then
                ' If there is excess whitespace, this will remove
                ' like "THE      WORD".
                Dim delim As Char() = {" "c}
                Dim lines As String() = sOut.Split(delim, StringSplitOptions.RemoveEmptyEntries)

                sOut = ""
                Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
                For Each s As String In lines
                    sb.Append(s)
                    sb.Append(" ")
                Next s
                Return sb.ToString().Trim()
            Else
                Return sOut
            End If

        End Function



#End Region

#Region "ToggleHtmlBR"

        Public Shared Function ToggleHtmlBR(ByVal text As String, ByVal isOn As Boolean) As String
            Dim outS As String = ""

            If isOn Then
                outS = text.Replace(Constants.vbLf, "<br />")
            Else
                ' TODO: do this with via regex
                '
                outS = text.Replace("<br />", Constants.vbLf)
                outS = text.Replace("<br>", Constants.vbLf)
                outS = text.Replace("<br >", Constants.vbLf)
            End If

            Return outS
        End Function

#End Region

#End Region




        Public Shared Function GetUniqueIdentifier() As String
            Dim SID As String
            Dim LC As String
            Dim LC1 As String
            Dim ss As String
            Dim year As String
            Dim day As String
            Dim hour As String
            Dim minute As String
            Dim sec As String
            Dim msec As String
            year = System.DateTime.Now.Year.ToString
            day = System.DateTime.Now.DayOfYear.ToString
            hour = System.DateTime.Now.Hour.ToString
            minute = DateTime.Now.Minute.ToString
            sec = DateTime.Now.Second.ToString
            msec = DateTime.Now.Millisecond.ToString
            If year.Length = 4 Then
                year = year.Substring(2, 2)
            End If
            LC1 = ""
            LC1 = LC1 + ChangStr(True, year, "0", 2)
            LC1 = LC1 + ChangStr(True, day, "0", 3)
            LC1 = LC1 + ChangStr(True, hour, "0", 2)
            LC1 = LC1 + ChangStr(True, sec, "0", 2)
            Dim t As Long
            t = Convert.ToInt64(LC1)
            LC1 = ""
            LC1 = IntToHex(t, 8)
            ss = ChangStr(True, sec, "0", 2)
            t = Convert.ToInt64(ss)
            LC1 = LC1 + IntToHex(t, 2)
            LC1 = LC1 + IntToHex(Convert.ToInt64(msec), 3)
            ss = System.Guid.NewGuid.ToString
            LC = ss.Substring(0, 8)
            LC = CheckID(LC)
            LC1 = LC1 + ChangStr(True, LC, "0", 8)
            ss = ""
            ss = LC1.Substring(1, 8)
            SID = HexTo36(ss, 6)
            ss = LC1.Substring(8, 13)
            SID = SID + HexTo36(ss, 10)
            Return SID
        End Function
#Region "---Gey Unique  Identifier---------"


        Private Shared Function IntToHex(ByVal num As Long, ByVal len As Integer) As String
            Dim lcHexStr As String
            Dim strHex As String
            lcHexStr = "0123456789ABCDEF"
            Dim lMod As Integer
            Dim len1 As Integer
            lMod = 0
            strHex = ""
            While num > 0
                lMod = CType((num Mod 16), Integer)
                num = num / 16
                strHex = lcHexStr.Substring(lMod, 1) + strHex
            End While
            len1 = strHex.Length
            If len1 < len Then
                strHex = ChangStr(True, strHex, "0", len)
            End If
            Return strHex
        End Function
        Private Shared Function ChangStr(ByVal bISF As Boolean, ByVal strHex As String, ByVal subStr As String, ByVal len As Integer) As String
            Dim len1 As Integer
            len1 = len - strHex.Length
            Dim i As Integer = 0
            While i < len1
                If bISF = True Then
                    strHex = subStr + strHex
                Else
                    strHex = strHex + subStr
                End If
                System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
            End While
            Return strHex
        End Function
        Private Shared Function CheckID(ByVal str As String) As String
            Dim outStr As String
            Dim charStr As String
            Dim len As Integer
            outStr = ""
            str = str.ToUpper
            len = str.Length
            Dim i As Integer = 0
            While i < len
                charStr = str(i)

                If (CInt("&h" + charStr) >= 48) AndAlso (CInt("&h" + charStr) <= 59) OrElse (CInt("&h" + charStr) >= 65) AndAlso (CInt("&h" + charStr) <= 70) Then

                    outStr = outStr + charStr
                Else
                    outStr = outStr + IntToHex(CInt("&h" + charStr), 0)
                End If
                System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
            End While
            If outStr.Length > 8 Then
                outStr = outStr.Substring(0, 8)
            End If
            Return outStr
        End Function
        Private Shared Function HexTo36(ByVal strHex As String, ByVal len As Integer) As String
            Dim str62 As String
            Dim lc62Str As String
            lc62Str = "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            str62 = ""
            Dim lMod As Integer = 0
            Dim num1 As Long = 0
            Dim lnsHex As Long = 1
            strHex = strHex.ToUpper
            Dim len1 As Integer
            len1 = strHex.Length
            While len1 > 0
                Dim ss As Char
                ss = strHex((len1 - 1))
                num1 = num1 + (lc62Str.IndexOf(ss)) * lnsHex
                If len1 = 1 Then
                    lnsHex = 16
                Else
                    lnsHex = lnsHex * 16
                End If
                System.Math.Max(System.Threading.Interlocked.Decrement(len1), len1 + 1)
            End While
            str62 = ""
            While num1 > 0
                lMod = CType((num1 Mod 36), Integer)
                num1 = num1 / 36
                str62 = lc62Str.Substring(lMod, 1) + str62
            End While
            len1 = str62.Length
            If len1 < len Then
                str62 = ChangStr(True, str62, "0", len)
            End If
            Return str62
        End Function

#End Region

#Region "------T-SQL------- "


        Public Shared Function IsDBNull(ByVal dr As IDataReader, ByVal name As String) As Boolean
            Dim index As Integer = dr.GetOrdinal(name)

            Return dr.IsDBNull(index)
        End Function
        Public Shared Function GetString(ByVal dr As IDataReader, ByVal name As String) As String
            If Not IsDBNull(dr, name) Then
                Return CType(dr(name), String)
            End If
            Return Nothing
        End Function
        Public Shared Function GetInt32(ByVal dr As IDataReader, ByVal name As String) As Integer
            If Not IsDBNull(dr, name) Then
                Return CType(dr(name), Integer)
            End If
            Return 0
        End Function
        Public Shared Function GetDateTime(ByVal dr As IDataReader, ByVal name As String) As DateTime
            If Not IsDBNull(dr, name) Then
                Return CType(dr(name), DateTime)
            End If
            Return DateTime.MinValue
        End Function


        Public Shared Function GetDecimal(ByVal dr As IDataReader, ByVal name As String) As Decimal
            If Not IsDBNull(dr, name) Then
                Return CType(dr(name), Decimal)
            End If
            Return Decimal.MinValue
        End Function


        Public Shared Function GetBoolean(ByVal dr As IDataReader, ByVal name As String) As Boolean
            If Not IsDBNull(dr, name) Then
                Return CType(dr(name), Boolean)
            End If
            Return False
        End Function
        Public Shared Function GetGuid(ByVal dr As IDataReader, ByVal name As String) As Guid
            If Not IsDBNull(dr, name) Then
                Return CType(dr(name), Guid)
            End If
            Return Guid.Empty
        End Function


#End Region

    End Class
End Namespace

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?