Public Class Frm_ConvertINI

    Public allini() As System.IO.FileInfo
    Dim StyleLista() As C_Epole.SStyle

    Public Function GetFileCount() As Short
        If Me.allini Is Nothing Then Return 0

        Try
            Return Me.allini.GetUpperBound(0)
        Catch ex As Exception
            Return 0
        End Try
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'GetFileList() '//모든파일정보를 수집
        Me.Label5.Text = "전체파일대상수 : " & allini.GetUpperBound(0)
        Me.Update()
        ReadFileToMem() '//파일의 데이터를 분석하여 데이터테이블로 변환한다.
        SaveDTtoNewFiletype()
        removeInifile()
        MsgBox("모든 변환이 완료되었습니다" & vbCrLf & "다시실행하세요", MsgBoxStyle.Information, "확인")
        Me.Close()
    End Sub
    Private Sub removeInifile()
        Dim Dir As New System.IO.DirectoryInfo(My.Application.Info.DirectoryPath & "\oldstylebak")
        If Not Dir.Exists Then Dir.Create()
        '//폴더이름빼오기
        Dim dd As String
        For Each F As System.IO.FileInfo In allini
            dd = Dir.FullName & "\" & F.Directory.Name
            If System.IO.Directory.Exists(dd) = False Then System.IO.Directory.CreateDirectory(dd)
            Try
                If System.IO.File.Exists(Dir.FullName & "\" & F.Directory.Name & "\" & F.Name) Then
                    System.IO.File.Delete(Dir.FullName & "\" & F.Directory.Name & "\" & F.Name)
                End If
                F.MoveTo(Dir.FullName & "\" & F.Directory.Name & "\" & F.Name)
            Catch ex As Exception
                MsgBox("INI파일 제거중 알수없는 오류발생" & vbCrLf & vbCrLf & ex.ToString, MsgBoxStyle.Critical, "확인")
            End Try
        Next
    End Sub
    Private Sub Addfile(ByVal filename As String)
        Try
            ReDim Preserve allini(allini.GetUpperBound(0) + 1)    '//배열증가
        Catch ex As Exception
            ReDim Preserve allini(0)    '//배열증가
        End Try
        Me.Update()

        allini(allini.GetUpperBound(0)) = New System.IO.FileInfo(filename)
    End Sub

    Private Sub ReadFileToMem()
        '//스타일목록에해당하는 배열을 가져온다.

        ReDim StyleLista(allini.GetUpperBound(0))

        Dim idx As Integer = 0

        Me.pb.Minimum = 0
        Me.pb.Value = 0
        Me.pb.Maximum = allini.GetUpperBound(0) + 1

        For Each Fi As System.IO.FileInfo In allini
            If pb.Value < pb.Maximum Then pb.Value += 1
            Msg(Fi.Name & "파일을 해석하고있습니다")
            Me.Update()
            FileToDataTable.ReadFile_OLD(StyleLista, Fi.FullName, idx)
            idx += 1
        Next
        Msg("모든파일을 해석하였습니다. 이제 저장합니다")


    End Sub

    Private Sub SaveDTtoNewFiletype()
        Dim ini As ACC.MyINI = Nothing
        Dim idx As Integer = 0
        Dim File As String = ""
        Dim Pfile As String = ""

        For Each SL As C_Epole.SStyle In StyleLista
            Me.pb.Value = 0
            Me.pb.Minimum = 0
            Me.pb.Maximum = SL.PATTERNTABLE.Rows.Count
            Msg(SL.Desc & " 스타일을 저장합니다")

            For Each DR As DataRow In SL.PATTERNTABLE.Rows
                If Me.pb.Value < Me.pb.Maximum Then Me.pb.Value += 1
                Me.Update()
                Try
                    File = DR("파일")
                Catch ex As Exception
                    File = ""
                End Try
                If File <> "" Then
                    If File <> Pfile Then
                        ini = New ACC.MyINI(ReplaceExt(Me.allini(idx).FullName, DR("파일")))
                        Pfile = File
                    End If
                    '//파일이 있는것들만
                    ini.Write(DR("섹션"), DR("개체") & DR("키"), DR("값"))
                End If
            Next
            idx += 1
        Next
        '//데이터테이블의 내용을 새로운 파일형태로 저장합니다.
    End Sub


    Private Sub Msg(ByVal mm As String)
        Me.Label6.Text = mm
        Me.Update()
    End Sub

    Public Sub GetFileList()
        If New IO.DirectoryInfo(My.Application.Info.DirectoryPath & "\stylejd").Exists Then
            For Each Fi As System.IO.FileInfo In New IO.DirectoryInfo(My.Application.Info.DirectoryPath & "\stylejd").GetFiles()
                '//ini가 아닌것들은 삭제를하고 ini는 배열에 넣는다
                If Fi.Extension.ToUpper = ".INI" Then Addfile(Fi.FullName)
            Next
        End If

        If New IO.DirectoryInfo(My.Application.Info.DirectoryPath & "\stylejj").Exists Then
            For Each Fi As System.IO.FileInfo In New IO.DirectoryInfo(My.Application.Info.DirectoryPath & "\stylejj").GetFiles()
                '//ini가 아닌것들은 삭제를하고 ini는 배열에 넣는다
                If Fi.Extension.ToUpper = ".INI" Then Addfile(Fi.FullName)
            Next
        End If

        If New IO.DirectoryInfo(My.Application.Info.DirectoryPath & "\stylekr").Exists Then
            For Each Fi As System.IO.FileInfo In New IO.DirectoryInfo(My.Application.Info.DirectoryPath & "\stylekr").GetFiles()
                '//ini가 아닌것들은 삭제를하고 ini는 배열에 넣는다
                If Fi.Extension.ToUpper = ".INI" Then Addfile(Fi.FullName)
            Next
        End If

        If New IO.DirectoryInfo(My.Application.Info.DirectoryPath & "\stylesk").Exists Then
            For Each Fi As System.IO.FileInfo In New IO.DirectoryInfo(My.Application.Info.DirectoryPath & "\stylesk").GetFiles()
                '//ini가 아닌것들은 삭제를하고 ini는 배열에 넣는다
                If Fi.Extension.ToUpper = ".INI" Then Addfile(Fi.FullName)
            Next
        End If

        If New IO.DirectoryInfo(My.Application.Info.DirectoryPath & "\stylets").Exists Then
            For Each Fi As System.IO.FileInfo In New IO.DirectoryInfo(My.Application.Info.DirectoryPath & "\stylets").GetFiles()
                '//ini가 아닌것들은 삭제를하고 ini는 배열에 넣는다
                If Fi.Extension.ToUpper = ".INI" Then Addfile(Fi.FullName)
            Next
        End If

    End Sub
End Class