自己常用方法封装

Imports System.Runtime.InteropServices
Imports System.Text
Imports System.IO
Imports Aras.IOM

Public Class CommonMethod

#Region "                              内容方法"
    ''' <summary>
    ''' 获取ini值
    ''' </summary>
    ''' <param name="section"></param>
    ''' <param name="key"></param>
    ''' <param name="def"></param>
    ''' <param name="retVal"></param>
    ''' <param name="size"></param>
    ''' <param name="filePath"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <DllImport("kernel32")> Private Shared Function GetPrivateProfileString(ByVal section As String, ByVal key As String, ByVal def As String, ByVal retVal As StringBuilder, ByVal size As Integer, ByVal filePath As String) As Integer
    End Function

    ''' <summary>
    ''' 写ini值
    ''' </summary>
    ''' <param name="section"></param>
    ''' <param name="value"></param>
    ''' <param name="key"></param>
    ''' <param name="filePath"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <DllImport("kernel32")> Private Shared Function WritePrivateProfileString(ByVal section As String, ByVal value As String, ByVal key As String, ByVal filePath As String) As Boolean
    End Function
#End Region

#Region "                              外部方法"
    ''' <summary>
    ''' 读取INI文件
    ''' </summary>
    ''' <param name="section">节点名称</param>
    ''' <param name="key">关键字</param>
    ''' <param name="path">文件路径</param>
    ''' <returns>返回值</returns>
    ''' <remarks></remarks>
    Public Function IniReadValue(ByVal section As String, ByVal key As String, ByVal path As String) As String
        Dim temp As StringBuilder = New StringBuilder(255)
        GetPrivateProfileString(section, key, "", temp, 255, path)
        Return temp.ToString()
    End Function

    ''' <summary>
    '''  写入INI文件
    ''' </summary>
    ''' <param name="section">节点名称</param>
    ''' <param name="key">关键字</param>
    ''' <param name="value">写入值</param>
    ''' <param name="path">文件路径</param>
    ''' <returns>返回值</returns>
    ''' <remarks></remarks>
    Public Function IniSetValue(ByVal section As String, ByVal key As String, ByVal value As String, ByVal path As String) As Boolean
        Dim IsSuccess As Boolean = WritePrivateProfileString(section, key, value, path)
        Return IsSuccess
    End Function

    ''' <summary>
    ''' 读取文件指定行数据
    ''' </summary>
    ''' <param name="Path">文件路径</param>
    ''' <param name="LineItm">指定行数</param>
    ''' <returns>返回值,超出索引返回最后一行值</returns>
    ''' <remarks></remarks>
    Public Function ReadValueByLine(ByVal Path As String, ByVal LineItm As Integer) As String
        Dim F3 As StreamReader = Nothing, StrReturn As String = Nothing
        Dim i As Integer = 0

        If LineItm < 1 Then Return StrReturn
        F3 = New StreamReader(Path, Encoding.Default)
        While Not F3.EndOfStream
            StrReturn = F3.ReadLine()
            i = i + 1
            If i = LineItm Then
                Exit While
            End If
        End While
        F3.Close()
        F3.Dispose()
        Return StrReturn
    End Function

    ''' <summary>
    ''' 登入Aras
    ''' </summary>
    ''' <param name="Url">链接地址</param>
    ''' <param name="DB">数据库</param>
    ''' <param name="Account">帐号</param>
    ''' <param name="Password">密码</param>
    ''' <returns>返回Innovator对象</returns>
    ''' <remarks></remarks>
    Public Function LoginAras(ByVal Url As String, ByVal DB As String, ByVal Account As String, ByVal Password As String, ByRef strError As String) As Innovator
        Dim Conn As HttpServerConnection = Nothing
        Dim Result As Item, Inn As Innovator = Nothing, startIndex As Integer

        Try
            Conn = IomFactory.CreateHttpServerConnection(Url, DB, Account, Password)
            If Conn Is Nothing Then Return Nothing
            Result = Conn.Login()
            If Result.isError() Then
                If Not Conn Is Nothing Then Conn.Logout()
                strError = Result.getErrorString()
                startIndex = (strError.IndexOf(":") + 1)
                If startIndex > 0 Then strError = strError.Substring(startIndex)
                If strError.Contains("Authentication") Then strError = "Invalid user or password" : Return Nothing
                If strError.Contains("Database") Then strError = "Database not available" : Return Nothing
            Else
                Inn = Result.getInnovator()
            End If
        Catch ex As Exception
            strError = ex.Message
            Return Nothing
        End Try
        Return Inn
    End Function

    ''' <summary>
    '''  验证是否成功登入 Aras
    ''' </summary>
    ''' <param name="Url">地址</param>
    ''' <param name="DB">数据库</param>
    ''' <param name="Account">帐号</param>
    ''' <param name="Password">密码</param>
    ''' <returns>返回布尔值</returns>
    ''' <remarks></remarks>
    Public Function VerifyLoginAras(ByVal Url As String, ByVal DB As String, ByVal Account As String, ByVal Password As String) As Boolean
        Dim Conn As HttpServerConnection = Nothing
        Dim Result As Item, Inn As Innovator = Nothing, IsSunccess As Boolean = False

        Try
            Conn = IomFactory.CreateHttpServerConnection(Url, DB, Account, Password)
            If Conn Is Nothing Then Return IsSunccess
            Result = Conn.Login()
            If Result.isError() Then
                If Not Conn Is Nothing Then Conn.Logout()
                Return IsSunccess
            Else
                Inn = Result.getInnovator()
                If Not Inn Is Nothing Then IsSunccess = True
            End If
        Catch ex As Exception
            IsSunccess = False
        End Try
        Return IsSunccess
    End Function

    ''' <summary>
    ''' 写Log文件LOG格式 [年-月-日 时:分:秒] | 错误原因 | 错误状态
    ''' </summary>
    ''' <param name="Path">日志文件路径</param>
    ''' <param name="Strings">错误原因</param>
    ''' <param name="dt">时间</param>
    ''' <param name="State">状态</param>
    ''' <remarks></remarks>
    Public Sub WriteLogFile(ByVal Path As String, ByVal Strings As String, ByVal dt As DateTime, ByVal State As String)
        Dim FilePath As String, f As FileStream, f2 As StreamWriter

        FilePath = New System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).Directory.FullName

        '如果不存在就创建file文件夹  
        If Not Directory.Exists(FilePath & "\log") Then Directory.CreateDirectory(FilePath & "\log")
        If Not File.Exists(Path) Then
            f = File.Create(Path)
            f.Close()
            f.Dispose()
        End If
        f2 = New StreamWriter(Path, True, Encoding.UTF8)
        f2.WriteLine("[" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "]" + " | " + Strings + " | " + State)
        f2.Close()
        f2.Dispose()

    End Sub

    ''' <summary>
    ''' 转换特殊字符 
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function TransString(ByVal value As String) As String
        If String.IsNullOrEmpty(value) Then Return value
        Return value.Trim().Replace("&", "&").Replace("'", "'").Replace("\", """)
    End Function

#End Region

End Class

posted on 2014-07-04 22:02  码农老K  阅读(241)  评论(0编辑  收藏  举报

导航