Imports System.IO
Imports System.Net
Imports System.Net.NetworkInformation

Public Class HX_PGS_UPDATE
    ReadOnly ds2 As New DataSet
    ReadOnly computerProperties As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties()
    ReadOnly IPEndPoint() As EndPoint = computerProperties.GetActiveTcpListeners()
    Dim myIPEndPoint As IPEndPoint
    Dim 內網線路 As Boolean : ReadOnly 開發線路 As Boolean
    Private Sub Set_清單1()
        ftp清單_dgv.DataSource = Nothing : ds2.Clear()
        ftp清單_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        ftp清單_dgv.ColumnHeadersHeight = 25
        ftp清單_dgv.AllowUserToAddRows = False
        SQL_FTP資料夾路徑分配()
        da.Fill(ds2) : ftp清單_dgv.DataSource = ds2.Tables(0) : conn.Close()
    End Sub
    Private Sub IP比對()
        '-----------------------IP比對----------------------------------------------------------------------------------------------------------------------------------------------------
        For Each myIPEndPoint In IPEndPoint
            If Strings.Left(myIPEndPoint.Address.ToString, 8) = "172.1.1." Then
                伺服器區域_cb.Text = "公司內網" : 內網線路 = True
                Exit For
            End If
        Next
        If 內網線路 = False And 開發線路 = False Then
            伺服器區域_cb.Text = "印尼外網"
        End If
    End Sub
    Private Sub HX_PGS_UPDATE_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        IP比對() : 資料數 = 11
        MyModule1.進度條() : If Strings.Left(伺服器區域_cb.Text, 2) = "印尼" Or Strings.Left(伺服器區域_cb.Text, 2) = "公司" Then : SQL_連線字串_正式() : Else : SQL_連線字串_測試() : End If
        MyModule1.進度條() : SQL_FTP伺服器登入位置()
        MyModule1.進度條() : If dr.Read() Then : Target = dr("FTP地址") : FTP帳號 = dr("FTP帳號") : FTP密碼 = dr("FTP密碼") : FTP物理位置 = dr("FTP物理位置") : End If : conn.Close()
        MyModule1.進度條() : Set_清單1()
        MyModule1.進度條() : Dim HH As String = ""
        For i As Integer = 0 To ftp清單_dgv.Rows.Count - 1
            If i < 10 Then : HH = "0" & i : ElseIf i >= 10 Then : HH = i : End If
            If ftp清單_dgv("指定", i).Value.ToString = "AA" & HH Then : AA(i) = ftp清單_dgv("資料夾名稱", i).Value.ToString : End If
        Next
        MyModule1.進度條() : Target1 = Target & AA(0) & "/"
    End Sub
    Private Sub HX_PGS_UPDATE_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        MyModule1.進度條() : Delay(30000)
        MyModule1.進度條() : Dim Str As String = Application.StartupPath
        MyModule1.進度條() : If File.Exists(Str + "\HX-PGS-ERP-SYS.exe") Then : File.Delete(Str + "\HX-PGS-ERP-SYS.exe") : End If
        MyModule1.進度條() : My.Computer.Network.DownloadFile(Target & AA(0) & "/" & "HX-PGS-ERP-SYS.exe", Str + "\HX-PGS-ERP-SYS.exe")
        MyModule1.進度條() : Shell(Str + "\HX-PGS-ERP-SYS.exe", vbNormalFocus)
        Me.Close()
    End Sub
    Private Sub 伺服器區域_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 伺服器區域_cb.SelectedIndexChanged
        '-----------------------確認登入伺服器位置-----------------------------------------------------------------------------------------------------------------------------------------
        If 伺服器區域_cb.Text = "印尼外網" Then
            登入人IP = "103.231.201.230,2433"
        ElseIf 伺服器區域_cb.Text = "公司內網" Then
            登入人IP = "172.1.1.10,2433"
        ElseIf 伺服器區域_cb.Text = "台灣開發" Then
            登入人IP = "106.1.50.79"
        End If
    End Sub
    Public Sub Delay(ByRef Interval As Double)  'Interval单位为毫秒
        Dim time As DateTime = DateTime.Now
        Dim Span As Double = Interval * 1234
        While ((DateTime.Now.Ticks - time.Ticks) < Span)
            Application.DoEvents()
        End While
    End Sub
End Class
