[vb.net Development] Function module > vb.net

본문 바로가기

vb.net

[Development] [vb.net Development] Function module

회원사진
하나를하더라도최선을
2020-05-06 20:07 4,714 0

본문



Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Threading
 
Module mFunction
 
    <DllImport("winmm.dll", EntryPoint:="timeGetTime")>
    Public Function timeGetTime() As UInteger
    End Function
 
    Public MainForm As Form
    Public Function LOG(ByVal value As String, Optional isEmptyLine As Boolean = False, Optional isMsgBox As Boolean = False, Optional isNoAdd As Boolean = FalseAs String
        If MainForm Is Nothing Then Return ""
        Dim LB As ListBox = CType(MainForm.Controls.Find("LOG_LIST"True).FirstOrDefault(), ListBox)
        StreamWriterString(value)
        Try
            If Not LB Is Nothing Then
                Do While LB.Items.Count > 1000
                    LB.Items.Remove(LB.Items(0))
                Loop
                If isEmptyLine Then LB.Items.Add("")
                If isNoAdd Then
                    LB.Items(LB.Items.Count - 1= value
                Else
                    LB.Items.Add(value)
                End If
                LB.SelectedIndex = LB.Items.Count - 1
            End If
            If isMsgBox Then MsgBox(value)
            Return True
        Catch ex As Exception
            el.WriteToErrorLog(ex.Message, ex.StackTrace, "LOG.ListBox")
            Return False
        Finally
        End Try
    End Function
 
    Public Sub StreamWriterString(ByVal LB As ListBox, ByVal value As String)
        Try
            Dim SUB_NAME As String = DateAndTime.Now.ToString("yyyy.MM.dd.HH")
            Using fs As FileStream = New FileStream(String.Concat("LOG\""LOG+", SUB_NAME, ".txt"), FileMode.Append, FileAccess.Write)
                Dim s As StreamWriter = New StreamWriter(fs)
                s.WriteLine(value)
                s.Close()
                fs.Close()
            End Using
        Catch ex As Exception
            el.WriteToErrorLog(ex.Message, ex.StackTrace, "StreamWriterString.value")
        End Try
    End Sub
 
    Public Function KillProcess(app_exe_Name As StringAs Integer
        Dim count As Integer
        Try
            For Each Process As Object In GetObject("winmgmts:").ExecQuery("Select * from Win32_Process Where Name = '" & app_exe_Name & "'")
                count += 1
                Process.Terminate()
            Next
        Catch ex As Exception
            count += KillProcess(app_exe_Name)
        End Try
        Return count
    End Function
 
    Public Sub Sleep(ByVal millisecond As Integer)
        Thread.Sleep(millisecond)
    End Sub
 
    Public APP_LOG_PATH As String = String.Concat(MyDocuments, Application.ProductName, "\LOG\"'// 프로그램 설정파일 저장위치
    Public Sub StreamWriterString(ByVal value As String)
 
        Dim SUB_NAME As String = DateAndTime.Now.ToString("yyyy.MM.dd-HH")
        Try
            Using fs As FileStream = New FileStream(APP_LOG_PATH & SUB_NAME & "+LOG.LOG", FileMode.Append, FileAccess.Write)
                Dim s As StreamWriter = New StreamWriter(fs)
                s.WriteLine(value)
                s.Close()
                fs.Close()
            End Using
        Catch ex As Exception
            el.WriteToErrorLog(ex.Message, ex.StackTrace, "StreamWriterString.value")
        End Try
    End Sub
 
    Public Sub StreamWriterString(ByVal fPath As StringByVal value As String)
        Try
            Using fs As FileStream = New FileStream(fPath, FileMode.Append, FileAccess.Write)
                Dim s As StreamWriter = New StreamWriter(fs)
                s.WriteLine(value)
                s.Close()
                fs.Close()
            End Using
        Catch ex As Exception
            el.WriteToErrorLog(ex.Message, ex.StackTrace, "StreamWriterString.Path.value")
        End Try
    End Sub
 
    Public Function Serialize(ByVal fName As StringByVal x As ObjectAs Boolean
        If fName.Length = 0 Then Return False
        Dim S() As String = fName.Split("\"c)
        S(S.Length - 1= EncryptString(S(S.Length - 1))
        fName = Join(S, "\")
        Dim BF As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
        Dim FS As New FileStream(String.Concat(MyDocuments, Application.ProductName, "\User Data\" & fName), FileMode.OpenOrCreate)
        Try
            BF.Serialize(FS, x) ' write to file
            Return True
        Catch ex As Exception
            Return False
        Finally
            FS.Dispose()
        End Try
    End Function
 
    Public Function Deserialize(ByVal fName As StringAs Object
        If fName.Length = 0 Then Return Nothing
        Dim S() As String = fName.Split("\"c)
        S(S.Length - 1) = EncryptString(S(S.Length - 1))
        fName = Join(S, "\")
        If Not File.Exists(String.Concat(MyDocuments, Application.ProductName, "\User Data\" & fName)) Then Return Nothing
        Dim BF As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
        Dim FS As New FileStream(String.Concat(MyDocuments, Application.ProductName, "\User Data\" & fName), FileMode.OpenOrCreate)
        Try
            Return BF.Deserialize(FS) ' write to file
        Catch ex As Exception
            Return Nothing
        Finally
            FS.Dispose()
        End Try
    End Function
 
    Public Function GetFile() As String
        Dim OFD As OpenFileDialog = New OpenFileDialog
        OFD.Filter = "Files|*.*"
        OFD.FileName = "Select a File"
        Dim result As DialogResult = OFD.ShowDialog()
        If result = Windows.Forms.DialogResult.OK Then
            Return System.IO.Path.GetFullPath(OFD.FileName)
        End If
        Return Nothing
    End Function
 
    Public Function GetPath() As String
        Dim FD As FolderBrowserDialog = New FolderBrowserDialog
        Dim result As DialogResult = FD.ShowDialog()
        If result = DialogResult.OK Then
            Return FD.SelectedPath & ""
        End If
        Return Nothing
    End Function
 
    Public Function DATAGRIDVIEW_DEFAULT_SETTING(ByRef DGV As DataGridView) As DataGridView
        DGV.AllowUserToAddRows = False
        DGV.RowHeadersVisible = False
        DGV.AllowUserToDeleteRows = False
        DGV.ColumnHeadersDefaultCellStyle.BackColor = Color.LightGray
        DGV.ColumnHeadersDefaultCellStyle.Font = New Font("맑은 고딕"9.0!, FontStyle.Bold)
        DGV.BackgroundColor = Color.White
        DGV.AutoResizeColumnHeadersHeight()
        'DGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        DGV.AutoResizeColumns()
 
        ''// CheckBox Column 추가
        'DataGridViewAddingCheckBoxColumn(DGV)
 
        For Each C As DataGridViewColumn In DGV.Columns
            C.SortMode = DataGridViewColumnSortMode.Programmatic    '// 해더 클릭시 Sort X
        Next
 
        Return DGV
    End Function
 
    Private Sub DataGridViewAddingCheckBoxColumn(ByVal DGV As DataGridView)
        Dim D As DataTable = DGV.DataSource
        If D Is Nothing Then Return
        If Not DGV.Columns.Contains("ck"Then
            If Not D.Columns.Contains("ck"Then
                D.Columns.Add("ck", GetType(Boolean))
                Dim chk As DataGridViewCheckBoxColumn = DGV.Columns("ck")
                chk.HeaderText = "체크"
                chk.DisplayIndex = 0
            End If
        End If
    End Sub
 
    Public Function HTMLOrganize(ByVal value As StringAs String
        value = Replace(value, vbCrLf, Space(1))
        value = Replace(value, vbCr, Space(1))
        value = Replace(value, vbLf, Space(1))
        value = Replace(value, vbTab, Space(1))
        value = Replace(value, Chr(0), Space(1))
        value = Replace(value, Chr(10), Space(1))
        Do While InStr(value, Space(2))
            value = Replace(value, Space(2), Space(1))
        Loop
        value = Replace(value, "> "">")
        value = Replace(value, " <""<")
        value = Replace(value, "'""""")
        Return value
    End Function
 
    Public Function UrlEncode(ByVal str As String, Optional ByVal ED As System.Text.Encoding = NothingAs String
        Return System.Net.WebUtility.UrlEncode(str)
    End Function
 
    Public Function euckr(ByVal str As StringAs String
        Dim res As String = Web.HttpUtility.UrlEncode(str, System.Text.Encoding.GetEncoding(51949))
        Dim S() As String = Split(res, "%")
        Dim T As String
        For i As Integer = 1 To S.Length - 1
            T = S(i).Substring(02).ToUpper
            S(i) = S(i).Substring(2, S(i).Length - 2)
            S(i) = String.Concat(T, S(i))
        Next
        Return Join(S, "%")
    End Function
 
    Public Function HTMLDEL(ByRef T As StringAs String
        Return System.Text.RegularExpressions.Regex.Replace(T, "<.*?>""")
    End Function
 
    Public Function textEN(ByVal T As StringAs String
        Dim Bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(T)
        Dim S(UBound(Bytes)) As String
        For i As Integer = 0 To UBound(Bytes)
            Select Case Chr(Bytes(i).ToString)
                Case vbNewLine, vbCrLf, vbCr, vbLf, vbNullChar, vbNullString, vbBack : S(i) = "%0D%0A"  '// 줄바꿈 외...
                Case "0" To "9" : S(i) = Chr(Bytes(i).ToString)    '// 숫자
                Case Space(1) : S(i) = "+"  '// 띄어쓰기
                Case "*""-""_" : S(i) = Chr(Bytes(i).ToString)    '// 특문
                Case "A" To "Z" : S(i) = Chr(Bytes(i).ToString)    '// 영대
                Case "a" To "z" : S(i) = Chr(Bytes(i).ToString)    '// 영소
                Case Else : S(i) = Join({"%", Hex(Bytes(i))}, vbNullString)
            End Select
        Next
        Return Join(S, vbNullString)
    End Function
 
    Private mRnd As New Random
    Public Function RandomInteger(ByVal s As Integer, ByVal e As Integer) As Integer
        Return mRnd.Next(s, e)
    End Function
 
 
    Public Function ReadText(fPATH As String, Optional ED As Encoding = NothingAs String
        Dim T As String = vbNullString
        If System.IO.File.Exists(fPATH) = True Then
            If ED Is Nothing Then ED = System.Text.Encoding.Default
            Try
                Using srStreamReader As New StreamReader(fPATH, ED)
                    T = srStreamReader.ReadToEnd()
                    srStreamReader.Close()
                End Using
            Catch ex As Exception
                LOG(ex.StackTrace)
                el.WriteToErrorLog(ex.Message, ex.StackTrace, "ReadText")
            End Try
        End If
        Return T
    End Function
 
    Public Function SaveText(fPATH As String, TEXT As String, Optional ED As Encoding = NothingAs Boolean
        Try
            If ED Is Nothing Then ED = System.Text.Encoding.Default
            Using File As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(fPATH, False, ED) '// False = 새로쓰기, True = 이어쓰기
                File.WriteLine(TEXT)
                File.Close()
            End Using
            Return True
        Catch ex As Exception
            LOG(ex.StackTrace)
            el.WriteToErrorLog(ex.Message, ex.StackTrace, "SaveText")
            Return False
        End Try
    End Function
 
End Module
 

댓글목록0

등록된 댓글이 없습니다.
게시판 전체검색