using System;
using System.Windows.Forms;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Timers;
using System.Net;
using System.Globalization;
using System.Threading;

// uyodbackup.exe
// ทำงานแบบ Console Application   Pipop ไว้แก้กลับด้วย
// ไม่จำเป็นต้องมี util.cs และ Time.cs 


namespace Original
{
    static class Program
    {
        public static string MyProg = "uyodbackup".ToLower();
        public static bool App4Console = true; // true ทำงานแบบ Console Application ,fause ทำงานแบบ Windows Application 
        public static string MyFolderApp = System.IO.Directory.GetCurrentDirectory();  // โพลเดอร์ทำงาน
        private static System.Timers.Timer bTimer;
        private static DateTime StartDate;
        private static string cTemp = "", cTemp2 = "";
        private static string MyApp = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
        private static string FldGdrive = "", mFlashDrive = "";
        private static string FldBDPos = @MyFolderApp+"\\";
        private static string Source4Gdrive = @"V:\_My Cloud\"; // เป็นของ Google Drive สำหรับ uyodback S
        private static string SourceMyGdrive = @"V:\_My Cloud\G Drive\"; // เป็นของ Google Drive สำหรับ uyodback S
        private static string MyCompany = "Customer";
        private static string cFle4Text = "", mRarText = "";
        private static int nShutdown = 0;
        public static int nForWork = 1;
        private static int nPress = 0;
        private static int nTime = 60;  // วินาที
        private static bool lUpdate = false;
        public static bool insource = false;
        public static bool inharddisk = false;
        public static string cFleWorkAct = "";  // ไว้ในในอนาคต ต่อไป 
        private static string cFle4Error = "_error.txt";
        private static bool nFirstBackup = false; // สำหรับตรวจสอบการสำรองครั้งแรก 
        public static bool lDebug = false;

        static void Init() // Pipop
        {
            lDebug = Special.DEBUG.Cr_Debug(MyProg);
        }


        static void Readme()
        {
            string Fle4Readme = FldGdrive + MyProg + "Readme.txt";
            TextWriter Logtxt = new StreamWriter(Fle4Readme);
            // การใช้งาน
            Logtxt.WriteLine("*" + MyProg + ".exe  ,DT : " + File.GetLastWriteTime(MyApp).ToString("dd/MM/yy HH:mm"));
            Logtxt.WriteLine(MyApp);
            Logtxt.WriteLine("");
            Logtxt.WriteLine("Variable");
            Logtxt.WriteLine("-FldBDPos  = " + FldBDPos);
            Logtxt.WriteLine("-FldGdrive = " + FldGdrive);
            Logtxt.WriteLine("-nShutdown    = " + nShutdown.ToString());
            Logtxt.WriteLine("-nTime        = " + nTime.ToString());
            Logtxt.WriteLine("");
            Logtxt.WriteLine("ชุดคำสั่ง ");
            Logtxt.WriteLine("uyodbackup     -> Backup For Customer");
            Logtxt.WriteLine("uyodbackup B_D -> Delete JunkFile In Bdpos And Source");
            Logtxt.WriteLine("uyodbackup C   -> Copy In copyfleintxt.txt");
            Logtxt.WriteLine("uyodbackup D   -> Delete In delfleintxt.txt");
            Logtxt.WriteLine("uyodbackup F   -> Backup For Source Code To Flashdrive");
            Logtxt.WriteLine("uyodbackup S   -> Backup For Source Code");
            Logtxt.WriteLine("uyodbackup W   -> Write uyodbackup.ini Again");
            Logtxt.WriteLine("Remark : Rar u C:\\Rar\\test *.dbf *.cdx ...");
            Logtxt.WriteLine("Remark : nShutdown=1 Shutdown =2 Restart");
            Logtxt.WriteLine(""); Logtxt.WriteLine("");

            Logtxt.WriteLine("File Error : " + cFle4Error); Logtxt.WriteLine(""); Logtxt.WriteLine("");

            string[] readText;
            cTemp = "uyodbackup.ini";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("uyodbackup.ini ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                readText = File.ReadAllLines(cTemp);
                int i = 0;
                Logtxt.WriteLine("**********");
                foreach (string s in readText)
                {
                    i++; if (s != "")
                    { Logtxt.WriteLine(s); if (i == 5) { Logtxt.WriteLine("**********"); } }
                }
                Logtxt.WriteLine((char)34 + FldGdrive + "Backup\\MyDBFOthertBackup.txt" + (char)34);
                Logtxt.WriteLine((char)34 + FldGdrive + "Backup\\MyEXEOthertBackup.txt" + (char)34);
                Logtxt.WriteLine((char)34 + FldGdrive + "Backup\\MyDoc4Customer.txt" + (char)34);
                Logtxt.WriteLine((char)34 + FldGdrive + "Backup\\FinnishCommand.txt" + (char)34);
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }


            cTemp = cFle4Text;
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("การทำงานตอน Backup");
                Logtxt.WriteLine("backup2c.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                Logtxt.WriteLine("**********");
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }

            cTemp = mRarText;
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("เพื่อ Unrar ไฟล์ *.rar");
                Logtxt.WriteLine("rarCommand.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                Logtxt.WriteLine("**********");
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }

            cTemp = @"R:\BdBackup\rarCommand.txt";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("เพื่อ Unrar ไฟล์ *.rar");
                Logtxt.WriteLine("rarCommand.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                Logtxt.WriteLine("**********");
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }

            cTemp = FldGdrive + "Backup\\" + "MyDBFOthertBackup.txt";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("MyDBFOthertBackup.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                Logtxt.WriteLine("โพลเดอร์อื่น เพื่อสำรองไฟล์ DBF อื่นๆ");
                Logtxt.WriteLine("**********");
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }

            cTemp = FldGdrive + "Backup\\" + "MyEXEOthertBackup.txt";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("MyEXEOthertBackup.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                Logtxt.WriteLine("โพลเดอร์ สำรองไฟล์ EXE อื่นๆ");
                Logtxt.WriteLine("**********");
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }

            cTemp = FldGdrive + "Backup\\" + "MyDoc4Customer.txt";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("MyDoc4Customer.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                Logtxt.WriteLine("ชื่อโพลเดอร์ สำรองไฟล์ชุด Office ของลูกค้า");
                Logtxt.WriteLine("**********");
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }

            cTemp = FldGdrive + "Backup\\" + "FinnishCommand.txt";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("FinnishCommand.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                Logtxt.WriteLine("**********");
                Logtxt.WriteLine("คำสั่ง DOS หลังจากทำงานเสร็จ");
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }

            cTemp = FldGdrive + "Uyodteam\\delfleintxt.txt";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("delfleintxt.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine("สำหรับ uyodbackup D");
                Logtxt.WriteLine(cTemp);
                Logtxt.WriteLine("**********");
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
            }
            else { Logtxt.WriteLine("สำหรับ uyodbackup D"); Logtxt.WriteLine(cTemp); }
            Logtxt.WriteLine(""); Logtxt.WriteLine("");

            cTemp = FldGdrive + "File List.txt";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("File List ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                Logtxt.WriteLine("**********");
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }

            cTemp = FldGdrive + "RunDosMode.txt";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("RunDosMode ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                Logtxt.WriteLine("**********");
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }

            cTemp = FldGdrive + "Backup\\" + "After2Execute.txt";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("After2Execute.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                Logtxt.WriteLine("**********");
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }

            cTemp = "xxxx.txt";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("xxxx.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                Logtxt.WriteLine("**********");
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }
            Logtxt.Close(); Logtxt.Dispose();
            if (Layout.Program.Password.Input_Password("Open Readme.txt",""))
            { Util.Program.OpenWebSite(Fle4Readme); Util.Program.OpenWebSite(cFle4Error); }
        }

        
        static void UyodMess(string cfWord)
        {
            MessageBox.Show(cfWord + System.Environment.NewLine
            + "" + System.Environment.NewLine
            + "" + System.Environment.NewLine
            , "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private static void Process2Arg(string mArg_1, String mArg_2, string FleRar)  
        {
            // FleRar สำหรับแสดงวันที่และเวลา
            if (nForWork == 0)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Command  : " + mArg_1 + " " + mArg_2);
                Console.ForegroundColor = ConsoleColor.White;
                return;
            }
            string txtmArg_2 = mArg_2.Trim(), txtmArg_3 = "";
            if (txtmArg_2.Length > 60) { txtmArg_2 = txtmArg_2.Substring(0, 60); }
            Console.WriteLine(mArg_1 + " " + txtmArg_2);
            Process PokProcess = new Process();
            PokProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            PokProcess.StartInfo.FileName = mArg_1;
            PokProcess.StartInfo.Arguments = mArg_2;
            PokProcess.StartInfo.UseShellExecute = false;
            PokProcess.StartInfo.CreateNoWindow = true;  
            PokProcess.StartInfo.RedirectStandardOutput = true;
            PokProcess.StartInfo.RedirectStandardInput = true;
            PokProcess.Start();
            if (FleRar != "" && File.Exists(FleRar)) { txtmArg_3 = " update " + File.GetLastWriteTime(FleRar).ToString("dd/MM/yy HH:mm"); }
            Write4Text(mArg_1 + " " + mArg_2, txtmArg_3);
            if (mArg_1.ToLower() == "rar") { txt2rarCommand(mArg_2); }
            if (nForWork == 2) { Console.WriteLine("Press Enter"); Console.ReadLine(); }
        }

        private static void txt2rarCommand(string cText) // เก็บการทำงาน rar 
        {
            if (File.Exists(mRarText))
            {
                using (StreamWriter MyReadme = File.AppendText(mRarText))  // เพิ่มไฟล์ไปเรื่อยๆ
                {
                    MyReadme.WriteLine("Rem unrar x -o+ " + cText + " " + (char)34
                        + Directory.GetCurrentDirectory() + (char)34 + rarNameFile(cText));
                    MyReadme.Close();
                }
            }
        }

        private static string rarNameFile(string cFleName)
        {
            string retFle = "", cTextCut = "", cText2 = "", cWord = "" + (char)34 + "";
            int nCut = 0;
            cFleName = cFleName.Trim();
            for (int i = 0; i < cFleName.Length; i++)
            {
                cTextCut = cFleName.Substring(i, 1);
                if (nCut > 0) // เจอคำที่ต้องการแล้ว
                {
                    if (cTextCut == cWord)
                    { nCut++; }
                    else { cText2 = cText2 + cTextCut; }
                }
                if (cTextCut == cWord && nCut == 0)
                { nCut++; }
                if (nCut >= 2) { break; } // เจอครบ 2 หลักออกเลย
            }
            if (cText2 != "")
            {
                cText2 = cText2 + ".rar";
                if (File.Exists(cText2))
                {
                    FileInfo fi = new FileInfo(cText2);
                    retFle = " ,Update : " + fi.LastWriteTime.ToString("dd/MM/yy HH:mm:ss");
                }
            }
            return retFle;
        }


        private static void StartTime()
        {
            StartDate = DateTime.Now;
            bTimer = new System.Timers.Timer(2000);
            bTimer.Elapsed += OnTimedEvent;
            bTimer.AutoReset = true;
            bTimer.Enabled = true;
            cTemp = "Start Backup " + " @" + StartDate.ToString("dd/MM/yy HH:mm:ss");
            Console.WriteLine(cTemp);
            Console.WriteLine("");
        }

        private static void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            Console.WriteLine(" @" + DateTime.Now.ToString("dd/MM/yy HH:mm"));
        }


        private static void FinishTime()
        {
            bTimer.Stop();
            bTimer.Dispose();
            Console.WriteLine("");
            cTemp = "Finish Backup" + " @" + DateTime.Now.ToString("dd/MM/yy HH:mm:ss");
            Console.WriteLine(cTemp);
            Write4Text(cTemp, "");
            TimeSpan ts = (DateTime.Now - StartDate);
            cTemp = "Calculate = " + ts.ToString(@"hh\:mm\:ss");
            Console.WriteLine(cTemp);
            Console.WriteLine("");
            Write4Text(cTemp, "");
            // แจงรายละเอียดเกี่ยวกับเครื่อง
            Write4Text("=======================", "");
            cTemp = "Machine : " + Environment.MachineName;
            Write4Text(cTemp, "");
            cTemp = "User    : " + Environment.UserName;
            Write4Text(cTemp, "");
            cTemp = "File    : " + System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            Write4Text(cTemp, "");
            TextWriter List2Write = new StreamWriter(FldGdrive + "File List.txt");
            DirectoryInfo dFolderList = new DirectoryInfo(FldBDPos);
            FileInfo[] FileList = dFolderList.GetFiles("*.exe", SearchOption.TopDirectoryOnly);
            List2Write.WriteLine("Folder : " + FldBDPos);
            List2Write.WriteLine("");

            int nYear = int.Parse(DateTime.Now.ToString("yyyy"));
            foreach (FileInfo cFileList in FileList)
            {
                if (int.Parse(cFileList.LastWriteTime.ToString("yyyy")) >= nYear)
                { List2Write.WriteLine(cFileList.Name + " ," + cFileList.LastWriteTime.ToString()); }

            }
            FileList = dFolderList.GetFiles("*.txt", SearchOption.TopDirectoryOnly);
            List2Write.WriteLine("");
            foreach (FileInfo cFileList in FileList)
            {
                if (int.Parse(cFileList.LastWriteTime.ToString("yyyy")) >= nYear)
                { List2Write.WriteLine(cFileList.Name + " ," + cFileList.LastWriteTime.ToString()); }

            }
            FileList = dFolderList.GetFiles("*.ini", SearchOption.TopDirectoryOnly);
            List2Write.WriteLine("");
            foreach (FileInfo cFileList in FileList)
            {
                if (int.Parse(cFileList.LastWriteTime.ToString("yyyy")) >= nYear)
                { List2Write.WriteLine(cFileList.Name + " ," + cFileList.LastWriteTime.ToString()); }

            }
            List2Write.Close();
            List2Write.Dispose();
        }


        private static void Write4Text(string ctxt, string ctxt2)
        {
            try
            {
                using (StreamWriter FleAddText = File.AppendText(cFle4Text))
                {
                    FleAddText.WriteLine(ctxt + " ==> " + (char)34 + Directory.GetCurrentDirectory() + (char)34 + " " + ctxt2);
                    FleAddText.Close();
                }
            }
            catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, ex.ToString()); ; return; }
        }


        static bool pressYesNO(string cSayText)
        {
            bool lYesno = false;
            Console.Write(cSayText + "  ..Press Y and Enter For Confirm  : ");
            string cYesno = Console.ReadLine();
            if (cYesno == null || cYesno == "") { cYesno = "N"; }
            cYesno = cYesno.ToUpper().Trim().Substring(0, 1);
            if (cYesno == "Y") { lYesno = true; } else { lYesno = false; }
            Console.Write(cYesno + " (" + lYesno.ToString().Trim() + ")");
            return lYesno;
        }


        static void ExeIndate(string FleExe)
        {
            string FleExeSource = FldBDPos + FleExe + ".exe";
            if (!File.Exists(FleExeSource)) { return; }
            string FleExeTarget = FldGdrive + "Backup\\Exe\\" + FleExe;
            FleExeTarget = FleExeTarget + File.GetLastWriteTime(FleExeSource).ToString("ddMMyyyy") + ".exe";
            if (!File.Exists(FleExeTarget))
            {
                try { File.Copy(FleExeSource, FleExeTarget); Console.WriteLine("Copy Complete ," + FleExeSource+" -> "+ FleExeTarget); }
                catch { Console.WriteLine("Copy Error ," + FleExeSource + " -> " + FleExeTarget); return; }
            }
        }


        static bool DRV4WorkAct() // ตรวจโพลเดอร์และ Drive 
        {
            bool n4WorkAct = true;
            cTemp = Path.GetPathRoot(FldGdrive);
            if (!Directory.Exists(cTemp)) { n4WorkAct = false; Console.Write("No Found Drive " + FldGdrive); Console.ReadLine(); }
            cTemp = Path.GetPathRoot(FldBDPos);
            if (!Directory.Exists(cTemp)) { n4WorkAct = false; Console.Write("No Found Drive " + FldBDPos); Console.ReadLine(); }
            if (n4WorkAct == false) { Console.Write("Please Edit uyodbackup.ini" + FldBDPos); Console.ReadLine(); }
            return n4WorkAct;
        }

        static void ChageFolder(string Fld4Chg)  // เปลี่ยนตำแหน่งโพลเดอร์
        { Directory.SetCurrentDirectory(Fld4Chg); }


        static void FirstBackup(string cFtext) // เตือนถ้าเป็นการสำรองครั้งแรก เพื่อความสมบูรณ์ของไฟล์
        { 
            Console.WriteLine(cFtext); Console.Write("....Press Any Key For Continue");  Console.ReadLine() ; 
        }


        static void Backup2Gdrive()   // เริ่มต้นการสำรอง BDPOS  และ BDPOSVFP 
        {
            string p= "Backup2Gdrive";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }  
            // ตรวจโพลเดอร์และ Drive 
            if (!DRV4WorkAct()) { return; }
            // 
            if (!Directory.Exists(FldBDPos)) { MessageBox.Show("ไม่พบ Folder " + FldBDPos); return; }
            if (!Directory.Exists(FldGdrive)) { Directory.CreateDirectory(FldGdrive); }
            // สร้างโพลเดอร์ 
            if (!Directory.Exists(FldGdrive + "Backup")) { Directory.CreateDirectory(FldGdrive + "Backup"); }
            if (!Directory.Exists(FldGdrive + "Backup\\Utility")) { Directory.CreateDirectory(FldGdrive + "Backup\\Utility"); }
            if (!Directory.Exists(FldGdrive + "Backup\\Environment")) { Directory.CreateDirectory(FldGdrive + "Backup\\Environment"); }
            if (!Directory.Exists(FldGdrive + "Backup\\Exe")) { Directory.CreateDirectory(FldGdrive + "Backup\\Exe"); nFirstBackup = true; }
            if (!Directory.Exists(FldGdrive + "Pdf")) { Directory.CreateDirectory(FldGdrive + "Pdf"); }
            if (!Directory.Exists(FldGdrive + "Screen")) { Directory.CreateDirectory(FldGdrive + "Screen"); }
            if (!Directory.Exists(FldGdrive + "Txt")) { Directory.CreateDirectory(FldGdrive + "Txt"); }
            if (!Directory.Exists(FldGdrive + "UyodTeam")) { Directory.CreateDirectory(FldGdrive + "UyodTeam"); }
            if (!Directory.Exists(FldGdrive + "Update")) { Directory.CreateDirectory(FldGdrive + "Update"); }
            if (!Directory.Exists(FldGdrive + "General")) { Directory.CreateDirectory(FldGdrive + "General"); }
            if (!Directory.Exists(FldGdrive + "Utility")) { Directory.CreateDirectory(FldGdrive + "Utility"); }
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            // เริ่มสำรองข้อมูล   
            Layout.Program.LayOut.BackGroud(3);
            if (nFirstBackup) { FirstBackup("First Backup"); }
            Console.WriteLine("");
            string mFolderBackup = FldGdrive + "Backup\\"; // เปลี่ยนเป็นโพลเดอร์ Backup 
            string mFolderSystem = mFolderBackup + "System\\";
            if (!Directory.Exists(mFolderSystem)) { Directory.CreateDirectory(mFolderSystem); }
            // ทำการสำรองครั้งแรก เพราะมีปัญหาเรื่องค้าง 
            if (lDebug) { Special.DEBUG.D(p," - 03"); }
            if (!File.Exists(mFolderBackup+"data.rar"))
            {
                if (!File.Exists("uyodbackupfirst.bat"))   
                {
                    TextWriter txt2first = new StreamWriter("uyodbackupfirst.bat");
                    txt2first.WriteLine(FldBDPos.Substring(0,2));
                    txt2first.WriteLine(@"Cd"+ FldBDPos.Substring(2));  

                    txt2first.WriteLine("");
                    txt2first.WriteLine("cd Data");
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderBackup + "data" + (char)34 + " *.dbf *.cdx *.fpt"); 
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderBackup + "data_t2" + (char)34 + " *.dbf *.cdx *.fpt");
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderBackup + "alldata" + (char)34 + " *.dbf *.cdx *.fpt"); 
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderBackup + "alldata_t2" + (char)34 + " *.dbf *.cdx *.fpt");

                    txt2first.WriteLine("");
                    txt2first.WriteLine("Cd.."); txt2first.WriteLine("Cd Transac");
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderBackup + "transac" + (char)34 + " *.dbf *.cdx *.fpt"); 
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderBackup + "transac_t2" + (char)34 + " *.dbf *.cdx *.fpt"); 
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderBackup + "alldata" + (char)34 + " *.dbf *.cdx *.fpt"); 
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderBackup + "alldata_t2" + (char)34 + " *.dbf *.cdx *.fpt"); 

                    txt2first.WriteLine("");
                    txt2first.WriteLine("Cd..");
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderSystem + "ini" + (char)34 + " *.ini"); 
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderSystem + "bat" + (char)34 + " *.bat"); 
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderSystem + "exe" + (char)34 + " *.exe"); 
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderSystem + "fox_exe" + (char)34 + " hovfp.exe posvfp.exe linkctrl.exe formail.exe pe*.exe");
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderSystem + "fox_exe" + (char)34 + " gmassage.exe master.exe poswin.exe");
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderSystem + "dbf" + (char)34 + " *.dbf *.cdx"); 
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderSystem + "prg" + (char)34 + " *.prg *.fxp"); 
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderSystem + "sct" + (char)34 + " *.sct *.scx"); 
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderSystem + "frx" + (char)34 + " *.frx *.frt"); 
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderSystem + "bmp" + (char)34 + " *.bmp *.jpg"); 
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderSystem + "txt" + (char)34 + " *.txt"); 
                    txt2first.WriteLine(MyFolderApp+"\\Rar u " + (char)34 + mFolderSystem + "bdposvfp" + (char)34 + " *.bat *.exe *.dbf *.cdx *.prg *.sc? *.fr? *.bmp *.jpg *.txt *.ini");
                    txt2first.WriteLine("Pause"); txt2first.WriteLine("exit");

                    txt2first.Close(); txt2first.Dispose();

                    if (Directory.Exists(FldBDPos) && !File.Exists(FldBDPos + "Rar.exe") && File.Exists("Rar.exe"))
                    { File.Copy("Rar.exe", FldBDPos + "Rar.exe", true); }
                }
                Util.Program.Pok_Mess("เป็นการสำรองครั้งแรก " + Environment.NewLine 
                    + Environment.NewLine+ "ให้เรียกไฟล์ uyodbackupfirst.bat");
                return;
            }
            if (lDebug) { Special.DEBUG.D(p," - 04"); }
            try
            {
                ChageFolder(FldBDPos + "Data");
                Process2Arg("Rar", "u " + (char)34 + mFolderBackup + "data" + (char)34 + " *.dbf *.cdx *.fpt", mFolderBackup + "data.rar");
                Process2Arg("Rar", "u " + (char)34 + mFolderBackup + "data_t2" + (char)34 + " *.dbf *.cdx *.fpt", mFolderBackup + "data_t2.rar");
                Process2Arg("Rar", "u " + (char)34 + mFolderBackup + "alldata" + (char)34 + " *.dbf *.cdx *.fpt", mFolderBackup + "alldata.rar");
                Process2Arg("Rar", "u " + (char)34 + mFolderBackup + "alldata_t2" + (char)34 + " *.dbf *.cdx *.fpt", mFolderBackup + "alldata_t2.rar");
                if (nFirstBackup) { FirstBackup("Backup Data --> Complete"); }
            }
            catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Error 1: " + ex.Message); }
            if (lDebug) { Special.DEBUG.D(p," - 05"); }
            try
            {
                ChageFolder(FldBDPos + "Transac");
                Process2Arg("Rar", "u " + (char)34 + mFolderBackup + "transac" + (char)34 + " *.dbf *.cdx *.fpt", mFolderBackup + "transac.rar");
                Process2Arg("Rar", "u " + (char)34 + mFolderBackup + "transac_t2" + (char)34 + " *.dbf *.cdx *.fpt", mFolderBackup + "transac_t2.rar");
                Process2Arg("Rar", "u " + (char)34 + mFolderBackup + "alldata" + (char)34 + " *.dbf *.cdx *.fpt", mFolderBackup + "alldata.rar");
                Process2Arg("Rar", "u " + (char)34 + mFolderBackup + "alldata_t2" + (char)34 + " *.dbf *.cdx *.fpt", mFolderBackup + "alldata_t2.rar");
                if (nFirstBackup) { FirstBackup("Backup Transac --> Complete"); }
            }
            catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Error 2: " + ex.Message);  }
            if (lDebug) { Special.DEBUG.D(p," - 06"); }
            try  // ข้อมูล Data ทั้งหมด  ตั้งแต่ Data2-Data8 และ Transac2-Transac8
            {
                string FleiData, iString;
                for (int i = 2; i < 9; i++)
                {
                    iString = i.ToString().Trim();
                    // Data HaveDbfandExeFile
                    FleiData = FldBDPos + "Data" + iString;
                    if (HaveDbfandExeFile(1, FleiData))
                    {
                        ChageFolder(FleiData);
                        Process2Arg("Rar", "u " + (char)34 + mFolderBackup + "data" + iString + (char)34 + " *.dbf *.cdx *.fpt", mFolderBackup + "data.rar");
                        if (nFirstBackup) { FirstBackup("Backup Data --> Complete"); }
                    }
                    // Transac
                    FleiData = FldBDPos + "Transac" + iString;
                    if (HaveDbfandExeFile(1, FleiData))
                    {
                        ChageFolder(FleiData);
                        Process2Arg("Rar", "u " + (char)34 + mFolderBackup + "transac" + iString + (char)34 + " *.dbf *.cdx *.fpt", mFolderBackup + "transac.rar");
                        if (nFirstBackup) { FirstBackup("Backup Transac --> Complete"); }
                    }
                }
            }
            catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Error 22: " + ex.Message);  }
            if (lDebug) { Special.DEBUG.D(p," - 07"); }
            // ตรวจสอบโพลเดอร์ที่มีไฟล์ plu.dbf และ roomtran.dbf  
            try
            {
                DirectoryInfo dFolderCloud = new DirectoryInfo(FldBDPos);
                FileInfo[] FileList; // = dFolderCloud.GetFiles("plu.dbf", SearchOption.AllDirectories);
                FileList = dFolderCloud.GetFiles("plu.dbf", SearchOption.AllDirectories);
                for (int iFile = 1; iFile <= 2; iFile++)
                {
                    switch (iFile)
                    {
                        case 2: FileList = dFolderCloud.GetFiles("roomtran.dbf", SearchOption.AllDirectories); break;
                    }
                    foreach (FileInfo cFileList in FileList) // หาโพลเดอร์ 
                    {
                        cTemp = cFileList.DirectoryName;
                        cTemp2 = cTemp.Substring(cTemp.LastIndexOf(@"\") + 1).ToLower();
                        if (cTemp2 == "data" || cTemp2 == "transac") { } // ไม่ต้องทำเพราะทำแล้ว
                        else
                        {
                            ChageFolder(cTemp);
                            Process2Arg("Rar", "u " + (char)34 + mFolderBackup + cTemp2 + (char)34 + " *.dbf *.cdx *.fpt", mFolderBackup + cTemp2 + ".rar");
                            if (nFirstBackup) { FirstBackup("Backup Data --> Complete"); }
                            // ตรวจสอบใน โพลเดอร์ Rooms ด้วย
                            cTemp = cTemp + @"\Rooms";
                            if (Directory.Exists(cTemp))
                            {
                                cTemp2 = cTemp2 + "_rooms";
                                ChageFolder(cTemp);
                                Process2Arg("Rar", "u " + (char)34 + mFolderBackup + cTemp2 + (char)34 + " *.dbf *.cdx *.fpt", mFolderBackup + cTemp2 + ".rar");
                                if (nFirstBackup) { FirstBackup("Backup Rooms --> Complete"); }
                            }
                        }
                    }
                }
            }
            catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Copy File Error 1: " + ex.Message);  }
            // 
            if (lDebug) { Special.DEBUG.D(p," - 08"); }
            try
            {
                ChageFolder(FldBDPos + "Dbfrun");
                Process2Arg("Rar", "u " + (char)34 + mFolderBackup + "dbfrun" + (char)34 + " *.dbf *.cdx", mFolderBackup + "dbfrun.rar");
                if (nFirstBackup) { FirstBackup("Backup Dbfrun --> Complete"); }
            }
            catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Error 31: " + ex.Message); }
            if (lDebug) { Special.DEBUG.D(p," - 09"); }
            if (Directory.Exists(FldBDPos + "Customer"))
            {
                try
                {
                    ChageFolder(FldBDPos + "Customer");
                    Process2Arg("Rar", "u " + (char)34 + mFolderBackup + "Customer" + (char)34 + " *.*", mFolderBackup + "Customer.rar");
                    if (nFirstBackup) { FirstBackup("Backup Customer --> Complete"); }
                }
                catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Error 32: " + ex.Message); }
            }
            if (lDebug) { Special.DEBUG.D(p," - 10"); }
            try
            {
                ChageFolder(FldBDPos + "Bakdata");
                Process2Arg("Rar", "u " + (char)34 + mFolderSystem + "dbf" + (char)34 + " *.dbf *.cdx", mFolderSystem + "dbf.rar");
                if (nFirstBackup) { FirstBackup("Backup Bakdata --> Complete"); }
            }
            catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Error 4: " + ex.Message); }
            try
            {
                ChageFolder(FldBDPos + "_Error");
                Process2Arg("Rar", "u " + (char)34 + mFolderSystem + "_Error" + (char)34 + " *.dbf *.cdx", mFolderSystem + "_Error.rar");
                if (nFirstBackup) { FirstBackup("Backup _Error --> Complete"); }
            }
            catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Error 5: " + ex.Message);  }
            if (lDebug) { Special.DEBUG.D(p," - 11"); }
            try
            {
                ChageFolder(FldBDPos);
                Process2Arg("Rar", "u " + (char)34 + mFolderSystem + "ini" + (char)34 + " *.ini", mFolderSystem + "ini.rar");
                if (nFirstBackup) { FirstBackup("Backup Init --> Complete"); }
                Process2Arg("Rar", "u " + (char)34 + mFolderSystem + "bat" + (char)34 + " *.bat", mFolderSystem + "bat.rar");
                if (nFirstBackup) { FirstBackup("Backup Bat --> Complete"); }
                Process2Arg("Rar", "u " + (char)34 + mFolderSystem + "exe" + (char)34 + " *.exe", mFolderSystem + "exe.rar");
                if (nFirstBackup) { FirstBackup("Backup Exe --> Complete"); }
                Process2Arg("Rar", "u " + (char)34 + mFolderSystem + "exe" + (char)34 + " hovfp.exe posvfp.exe linkctrl.exe formail.exe pe*.exe", mFolderSystem + "fox_exe.rar");
                Process2Arg("Rar", "u " + (char)34 + mFolderSystem + "exe" + (char)34 + " gmassage.exe master.exe poswin.exe", mFolderSystem + "fox_exe.rar");
                if (nFirstBackup) { FirstBackup("Backup Exe --> Complete"); }
                Process2Arg("Rar", "u " + (char)34 + mFolderSystem + "dbf" + (char)34 + " *.dbf *.cdx", mFolderSystem + "dbf.rar");
                if (nFirstBackup) { FirstBackup("Backup Dbf --> Complete"); }
                Process2Arg("Rar", "u " + (char)34 + mFolderSystem + "prg" + (char)34 + " *.prg *.fxp", mFolderSystem + "prg.rar");
                if (nFirstBackup) { FirstBackup("Backup Prg --> Complete"); }
                Process2Arg("Rar", "u " + (char)34 + mFolderSystem + "sct" + (char)34 + " *.sct *.scx", mFolderSystem + "sct.rar");
                if (nFirstBackup) { FirstBackup("Backup Sct --> Complete"); }
                Process2Arg("Rar", "u " + (char)34 + mFolderSystem + "frx" + (char)34 + " *.frx *.frt", mFolderSystem + "frx.rar");
                if (nFirstBackup) { FirstBackup("Backup Frx --> Complete"); }
                Process2Arg("Rar", "u " + (char)34 + mFolderSystem + "bmp" + (char)34 + " *.bmp *.jpg", mFolderSystem + "bmp.rar");
                if (nFirstBackup) { FirstBackup("Backup Bmp --> Complete"); }
                Process2Arg("Rar", "u " + (char)34 + mFolderSystem + "txt" + (char)34 + " *.txt", mFolderSystem + "txt.rar");
                if (nFirstBackup) { FirstBackup("Backup Txt --> Complete"); }
                Process2Arg("Rar", "u " + (char)34 + mFolderSystem + "bdposvfp" + (char)34 + " *.bat *.exe *.dbf *.cdx *.prg *.sc? *.fr? *.bmp *.jpg *.txt *.ini", mFolderSystem + "bdposvfp.rar");
                if (nFirstBackup) { FirstBackup("Backup BdPosVFP --> Complete"); }
            }
            catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Error 6: " + ex.Message); }
            if (lDebug) { Special.DEBUG.D(p," - 12"); }
            if (Directory.Exists(FldBDPos + "Transac\\rooms"))
            {
                try
                {
                    ChageFolder(FldBDPos + "Transac\\rooms");
                    Process2Arg("Rar", "u " + (char)34 + mFolderSystem + "rooms" + (char)34 + " *.dbf *.cdx", mFolderSystem + "rooms.rar");
                    if (nFirstBackup) { FirstBackup("Backup Rooms --> Complete"); }
                }
                catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Error 7: " + ex.Message); }
            }
            if (lDebug) { Special.DEBUG.D(p," - 03"); }
            if (Directory.Exists(FldBDPos + "Data\\001"))
            {
                try
                {
                    ChageFolder(FldBDPos + "Data\\001");
                    Process2Arg("Rar", "u " + (char)34 + mFolderSystem + "data001" + (char)34 + " *.dbf *.cdx", mFolderSystem + "data001.rar");
                    if (nFirstBackup) { FirstBackup("Backup Data001 --> Complete"); }
                }
                catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Error 8: " + ex.Message);  }
            }

            if (lDebug) { Special.DEBUG.D(p," - 14"); }
            // ตรวจสอบ Drive M
            if (Directory.Exists("M:\\Data"))
            {
                try
                {
                    ChageFolder("M:\\Data");
                    Process2Arg("Rar", "u " + (char)34 + mFolderBackup + "mdata" + (char)34 + " *.dbf *.cdx", mFolderBackup + "mdata.rar");
                    Process2Arg("Rar", "u " + (char)34 + mFolderBackup + "malldata" + (char)34 + " *.dbf *.cdx", mFolderBackup + "malldata.rar");
                    if (nFirstBackup) { FirstBackup("Backup mData --> Complete"); }
                }
                catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Error 9: " + ex.Message); }
            }
            if (Directory.Exists("M:\\Transac"))
            {
                try
                {
                    ChageFolder(FldBDPos + "M:\\Transac");
                    Process2Arg("Rar", "u " + (char)34 + mFolderBackup + "mtransac" + (char)34 + " *.dbf *.cdx", mFolderBackup + "mtransac.rar");
                    Process2Arg("Rar", "u " + (char)34 + mFolderBackup + "malldata" + (char)34 + " *.dbf *.cdx", mFolderBackup + "malldata.rar");
                    if (nFirstBackup) { FirstBackup("Backup Mtransac --> Complete"); }
                }
                catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Error 10: " + ex.Message);  }
            }
            if (Directory.Exists("M:\\Data"))
            {
                try
                {
                    ChageFolder(FldBDPos + "M:\\");
                    Process2Arg("Rar", "u " + (char)34 + mFolderBackup + "mrepo" + (char)34 + " *.fr?", mFolderBackup + "mrepo.rar");
                    if (nFirstBackup) { FirstBackup("Backup mReport --> Complete"); }
                }
                catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Error 11: " + ex.Message); }
            }
            // จบตรวจสอบ Drive M
            if (lDebug) { Special.DEBUG.D(p," - 15"); }
            ReadOnLyFleBack(); // อ่านเฉยๆเพื่อวัน/เวลาอัพไฟล์ 
            Console.WriteLine("");
            if (lDebug) { Special.DEBUG.D(p," - 16"); }
            FileInfo fi = new FileInfo("uyodbackup.ini");
            if (Util.Program.bTStime(4, fi.LastWriteTime, 7))  // ห่างจากประมาณ 7 วัน ให้ระบบ Backup นิ่งก่อน 
            {
                // สำรองอีกครั้ง ในแต่ละวัน    
                cTemp = FldGdrive + "Backup\\" + StartDate.ToString("ddd") + "\\"; // แสดงเป็น วัน 			 
                if (!Directory.Exists(cTemp)) { Directory.CreateDirectory(cTemp); }
                    DirectoryInfo dFolderCloud;
                    FileInfo[] FileList;
                    dFolderCloud = new DirectoryInfo(mFolderBackup);
                    FileList = dFolderCloud.GetFiles("*.rar", SearchOption.TopDirectoryOnly);
                    foreach (FileInfo cFileList in FileList)
                    {
                        if (CompareFile(cFileList.FullName, cTemp + cFileList))
                        {
                            if (Util.Program.CalFileSize(cFileList.FullName) <= 50) // กัน Rar ไม่ทัน 
                            {
                                Console.WriteLine(cFileList.FullName + " rar ไม่ทันข้ามไปก่อน");
                                if (inharddisk) { Console.ReadLine(); }
                            }
                            else  
                            {
                                try
                                { File.Copy(cFileList.FullName, cTemp + cFileList, true);
                                Console.WriteLine("Copy Complete ," + cTemp + cFileList+ " , DATE : " + cFileList.LastWriteTime.ToString("dd/MM/yyyy HH:mm:ss")
                                + " , SIZE : " + CalFileSize(cFileList.Length));
                            }
                            catch { Console.WriteLine("Copy Error ,"+ cFileList.FullName + " -> " + cTemp + cFileList); }
                            }
                        }
                 }

                cTemp = FldGdrive + "Backup\\" + StartDate.ToString("ddd") + "\\System\\"; // แสดงเป็น วัน 			 
                if (lDebug) { Special.DEBUG.D(p," - 17"); }
                if (!Directory.Exists(cTemp)) { Directory.CreateDirectory(cTemp); }
                    dFolderCloud = new DirectoryInfo(mFolderBackup + "System");
                    FileList = dFolderCloud.GetFiles("*.rar", SearchOption.TopDirectoryOnly);
                    foreach (FileInfo cFileList in FileList)
                    {
                        if (CompareFile(cFileList.FullName, cTemp + cFileList))
                        {
                        try {
                            File.Copy(cFileList.FullName, cTemp + cFileList, true);
                            Console.WriteLine("Copy Complete ," + cFileList + " , DATE : " + cFileList.LastWriteTime.ToString("dd/MM/yyyy HH:mm:ss")
                                           + " , SIZE : " + CalFileSize(cFileList.Length)); ;
                        }
                        catch { Console.WriteLine("Copy Error," + cFileList.FullName + " -> " + cTemp + cFileList); }
                        }
                    }
            }
            ExeIndate("posvfp"); ExeIndate("hovfp"); ExeIndate("formail");
            ExeIndate("gback"); ExeIndate("gfront"); ExeIndate("gcus");
            ExeIndate("ls"); ExeIndate("pe"); ExeIndate("gl");
            if (lDebug) { Special.DEBUG.D(p," - 18"); }
            DelFLEInDay(); // ลบไฟล์ขยะที่เกิน XX วัน 
            UyodTeamIsText();
            if (lDebug) { Special.DEBUG.D(p," - 19 *FINNISH"); }
        }

        // 
        static bool HaveDbfandExeFile(int nOption, string FoldeReturn)
        {
            bool lHave;
            FoldeReturn = FoldeReturn.Trim();
            if (Directory.Exists(FoldeReturn)) { lHave = true; } else { lHave = false; }
            if (lHave) // ตรวจFolder 
            {
                DirectoryInfo InFolder;
                InFolder = new DirectoryInfo(FoldeReturn);
                FileInfo[] FileList;
                switch (nOption)
                {
                    case (1): // ตรวจสอบไฟล์ DBF CDX
                        FileList = InFolder.GetFiles("*.dbf", SearchOption.TopDirectoryOnly);
                        break;
                    case (2): // ตรวจสอบไฟล์ EXE 
                        FileList = InFolder.GetFiles("*.exe", SearchOption.TopDirectoryOnly);
                        break;
                    case (3): // ตรวจสอบไฟล์ Office
                        FileList = InFolder.GetFiles("*.docx", SearchOption.TopDirectoryOnly);
                        break;
                    default: // BUG
                        FileList = InFolder.GetFiles("*.abc", SearchOption.TopDirectoryOnly);
                        break;
                }
                lHave = false;
                foreach (FileInfo cFileList in FileList) { lHave = true; }
            }
            return lHave;
        }



        private static String CalFileSize(double nFileSize) // คำนวนขนาดไฟล์
        {
            string[] sizes = { "B", "KB", "MB", "GB", "TB" };
            int order = 0;
            while (nFileSize >= 1024 && order < sizes.Length - 1)
            {
                order++;
                nFileSize = nFileSize / 1024;
            }
            return String.Format("{0:0.##} {1}", nFileSize, sizes[order]);
        }


        static void UyodTeamIsText()  // สำรองข้อมูลรูปแบบ Text ไปคลาวด์
        {
            UyodTeam2Text("Amphan", 1);
            UyodTeam2Text("Spec4you", 1);
            UyodTeam2Text("Screen4You", 1);
            UyodTeam2Text("Repo4POK", 2);
        }


        static void UyodTeam2Text(string Fletxt, int nFleType) // สำรองข้อมูลรูปแบบ Text ไปคลาวด์ 2
        {
            string p= "UyodTeam2Text";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            string FleSource, FleTarget;
            Fletxt = Fletxt.Trim();
            FleSource = FldBDPos + Fletxt;
            switch (nFleType)
            {
                case (2): FleSource = FleSource + ".txt"; break;
                default: FleSource = FleSource + ".prg"; break;
            }
            FleTarget = FldGdrive + "UyodTeam\\" + Fletxt + ".txt";
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            if (File.Exists(FleSource))
            {
                try
                {
                    // ตรวจสอบถ้าไม่มี 
                    if (!File.Exists(FleTarget)) { File.Copy(FleSource, FleTarget); }
                    // ตรวจสอบวันที่ล่าสุด 
                    if (File.GetLastWriteTime(FleSource) > File.GetLastWriteTime(FleTarget)) { File.Copy(FleSource, FleTarget, true); }
                }
                catch (Exception ex) { Console.WriteLine("Update File Error : " + ex.Message); }
            }

            // เปลี่ยนเป็นตรวจสอบจากคลาวด์บ้าง
            FleSource = FldGdrive + "Backup\\" + Fletxt + ".upd";
            if (lDebug) { Special.DEBUG.D(p," - 03"); }
            string FleMove = FldGdrive + "Backup\\" + Fletxt + "." + DateTime.Now.ToString("ddMMyy HHmmss");
            FleTarget = FldBDPos + Fletxt;

            switch (nFleType)
            {
                case (2): FleTarget = FleTarget + ".txt"; break;
                default: FleTarget = FleTarget + ".prg"; break;
            }
            // UyodMess("FleSource = "+FleSource + ", FleTarget = " + FleTarget);
            if (lDebug) { Special.DEBUG.D(p," - 04"); }
            if (File.Exists(FleSource))
            {
                try
                {
                    File.Copy(FleSource, FleTarget, true);
                    File.Move(FleSource, FleMove);
                }
                catch (Exception ex) { Console.WriteLine("Update File Error : " + ex.Message) ;  }
            }
            if (lDebug) { Special.DEBUG.D(p, " - 05 *FINNISH"); }
        }


        static bool ChkFle2DEL(string FleCheck) // ตรวจสอบไฟล์ที่ห้ามลบ  
        {
            bool lFledelete = true;
            string cFleCheck = FleCheck.ToUpper().Trim();
            cFleCheck = cFleCheck.Substring(0, cFleCheck.Length - 4);
            // ไฟล์ที่ไม่ต้องลบ				
            switch (cFleCheck)
            {
                case ("FILEHAVE"):
                case ("FOXUSER"):
                    lFledelete = false;
                    // UyodMess("File No Delete : " + cFleCheck);
                    break;
            }
            return lFledelete;
        }



        static void DelFLEInDay() // ลบไฟล์ขยะที่เกิน XX วัน 
        {
            // ส่วนของโปรแกรม
            string p = "DelFLEInDay";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            Del4FLEInDay(FldBDPos + "Backup", 7, "*.*");
            Del4FLEInDay(FldBDPos + "Bakdata", 7, "*.*");
            Del4FLEInDay(FldBDPos + "Error", 7, "*.*");
            Del4FLEInDay(FldBDPos + "_Error", 7, "*.*");
            Del4FLEInDay(FldBDPos + "Temp", 3, "*.*");
            Del4FLEInDay(FldBDPos + Environment.UserName.Trim() + "\\Temp", 3, "*.*");
            // ส่วนของสำรองของระบบคลาวด์
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            Del4FLEInDay(FldGdrive + "Pdf", 90, "*.*");
            Del4FLEInDay(FldGdrive + "Txt", 90, "*.*");
            Del4FLEInDay(FldGdrive + "Update", 90, "*.*");
            Del4FLEInDay(FldGdrive + "Backup\\Nov", 90, "*.*");
            Del4FLEInDay(FldGdrive + "Backup\\Oct", 90, "*.*");
            // ไฟล์ช่วยทำงานระบบคุมไฟ  
            Del4FLEInDay("\\Temp", 7, "tCard*.txt"); Del4FLEInDay("D:\\Temp", 7, "tCard*.txt");
            Del4FLEInDay(MyFolderApp+"\\History", 7, "Open*.txt");
            if (lDebug) { Special.DEBUG.D(p, " - 03 *FINNISH"); }
        }


        static void Del4FLEInDay(string Fodl4Delete, int nDay4Delete,string cScope) // ลบไฟล์ขยะที่เกิน XX วัน 
        {
            string p = "Del4FLEInDay";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            if (!Directory.Exists(Fodl4Delete)) { return; }
            if (cScope=="") { return; } cScope = cScope.Trim();
            TimeSpan ts;
            int nday;
            string cprename = "", csubname = "";
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            DirectoryInfo dFolderProg = new DirectoryInfo(Fodl4Delete);
            FileInfo[] FileList = dFolderProg.GetFiles(cScope, SearchOption.AllDirectories); //  cScope ,  *.*
            foreach (FileInfo cFileList in FileList)
            {
                csubname = cFileList.FullName.Trim();
                csubname = csubname.Substring(csubname.Length - 4);
                switch (csubname.ToLower())
                {
                    case (".cdx"): break;  // ไม่ต้องลบ เพราะมีผลกับไฟล์ DBF
                    case (".fpt"): break;  // ไม่ต้องลบ เพราะมีผลกับไฟล์ DBF
                    default:
                        ts = (DateTime.Now - File.GetLastWriteTime(cFileList.FullName));
                        nday = Convert.ToInt16(ts.TotalDays);
                        if (nday > nDay4Delete && ChkFle2DEL(cFileList.Name))
                        {
                            // เริ่มลบ
                            try {
                                File.Delete(cFileList.FullName);
                                if (csubname.ToLower() == ".dbf") // ถ้าพบเป็นไฟล์ .dbf ต้องลบ .cdx และ .fpt ไปด้วย
                                {
                                    cprename = cFileList.FullName.Trim();
                                    cprename = cprename.Substring(0, cprename.Length - 4);
                                    if (File.Exists(cprename + ".cdx")) { File.Delete(cprename + ".cdx"); }
                                    if (File.Exists(cprename + ".fpt")) { File.Delete(cprename + ".fpt"); }
                                }
                            }
                            catch { Console.WriteLine(cFileList.FullName+" is Not Delete"); }
                        }
                        break;
                }
            }
            if (lDebug) { Special.DEBUG.D(p, " - 03 *FINNISH"); }
        }


        static void UnRar2BdPos()  //ตรวจสอบไฟล์ปรับปรุงต่างๆ จะมีนามสกุล  Update\\*.rar   เพื่อ Unrar  
        {
            string p = "UnRar2BdPos";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            if (!File.Exists("UnRar2BdPos.noinput")) { return; }  // ฟังก์ชันนี้ไม่ต้องใช้แล้ว มันจะไปซ้ำกับ _updatesoftware.exe
            lUpdate = false; // สำหรับโปรแกรมตัวใหม่ 
            // ตรวจโพลเดอร์และ Drive 
            if (!DRV4WorkAct()) { return; }
            string mFolderUpdate = FldGdrive + "Update\\";
            if (!Directory.Exists(mFolderUpdate)) { Directory.CreateDirectory(mFolderUpdate); }
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            if (!File.Exists(mFolderUpdate + "Readme.txt"))
            {
                TextWriter Readme2Txt = new StreamWriter(mFolderUpdate + "Readme.txt");
                Readme2Txt.WriteLine("คำสั่งที่ใช้ Rar u filerar *.dbf *.cdx ...");
                Readme2Txt.WriteLine("");
                Readme2Txt.WriteLine("-update.rar ,bdpos.rar ,bdposvfp.rar For Folder : " + FldBDPos);
                Readme2Txt.WriteLine("-dbfrun.rar For Folder : " + FldBDPos + "Dbfrun");
                Readme2Txt.WriteLine("-data.rar For Folder : " + FldBDPos + "Data");
                Readme2Txt.WriteLine("-transac.rar For Folder : " + FldBDPos + "Transac");
                Readme2Txt.WriteLine("-data2sc.rar For Folder : " + FldBDPos + "Data2sc");
                Readme2Txt.WriteLine("-dec.rar For Folder : " + mFolderUpdate + "DEC");
                Readme2Txt.WriteLine("");
                Readme2Txt.WriteLine("");
                Readme2Txt.WriteLine("*.exe ,*.txt  ,*.ini For Update By Programn");
                Readme2Txt.Close();
                Readme2Txt.Dispose();
            }
            string FleSoftupdate = "Softupdate.bat";
            if (lDebug) { Special.DEBUG.D(p," - 03"); }
            DirectoryInfo dFolderUpdate = new DirectoryInfo(mFolderUpdate);  // 
            FileInfo[] FileList = dFolderUpdate.GetFiles("*.rar", SearchOption.TopDirectoryOnly);
            foreach (FileInfo cFileList in FileList)
            { Console.WriteLine("Found " + cFileList.FullName + " For Update "); lUpdate = true; } // สำหรับโปรแกรมตัวใหม่ 
            string FleSource, FleTarget, FleShortname, FleSourceMove;
            if (lUpdate)
            {
                if (!File.Exists(FleSoftupdate))
                {
                    TextWriter Print2Txt = new StreamWriter(FleSoftupdate);
                    Print2Txt.WriteLine("if Exist " + (char)34 + mFolderUpdate + "update.rar" + (char)34 + " unrar x -o+ " + (char)34 + mFolderUpdate + "update" + (char)34 + " " + FldBDPos);
                    Print2Txt.WriteLine("if Exist " + (char)34 + mFolderUpdate + "bdposvfp.rar" + (char)34 + " unrar x -o+ " + (char)34 + mFolderUpdate + "bdposvfp" + (char)34 + " " + FldBDPos);
                    Print2Txt.WriteLine("if Exist " + (char)34 + mFolderUpdate + "dbfrun.rar" + (char)34 + " unrar x -o+ " + (char)34 + mFolderUpdate + "dbfrun" + (char)34 + " " + FldBDPos + "Dbfrun");
                    Print2Txt.WriteLine("if Exist " + (char)34 + mFolderUpdate + "data.rar" + (char)34 + " unrar x -o+ " + (char)34 + mFolderUpdate + "data" + (char)34 + " " + FldBDPos + "Data");
                    Print2Txt.WriteLine("if Exist " + (char)34 + mFolderUpdate + "Transac.rar" + (char)34 + " unrar x -o+ " + (char)34 + mFolderUpdate + "transac" + (char)34 + " " + FldBDPos + "Transac");
                    Print2Txt.WriteLine("if Exist " + (char)34 + mFolderUpdate + "data2sc.rar" + (char)34 + " unrar x -o+ " + (char)34 + mFolderUpdate + "data2sc" + (char)34 + " " + FldBDPos + "Data2sc");
                    Print2Txt.WriteLine("if Exist " + (char)34 + mFolderUpdate + "dec.rar" + (char)34 + " unrar x -o+ " + (char)34 + mFolderUpdate + "dec" + (char)34 + " " + (char)34 + FldGdrive + "Backup\\DEC" + (char)34);
                    Print2Txt.Close();
                }
                if (lDebug) { Special.DEBUG.D(p," - 04"); }
                ReadFileDos(FleSoftupdate);  //Process.Start(FleSoftupdate);
                FleSoftupdate = "RenameInFuture.txt";  // สร้างไฟล์นี้ เพื่อจะเปลี่ยนชือ เมือเข้าโปรแกรมครั้งต่อไป
                if (lDebug) { Special.DEBUG.D(p," - 02"); }
                TextWriter Print2DEL = new StreamWriter(FleSoftupdate);
                foreach (FileInfo cFileList in FileList)
                {
                    FleSource = cFileList.FullName;
                    FleTarget = cFileList.Name.Trim();
                    FleTarget = FleTarget.Substring(0, FleTarget.Length - 4) + "." + DateTime.Now.ToString("ddMMyyHHmmss");
                    // File.Move(FleSource, FleTarget);  พบปัญหาเรื่องความเร็ว
                    Print2DEL.WriteLine("Rename " + (char)34 + FleSource + (char)34 + " " + (char)34 + FleTarget + (char)34);
                    Write4Text("Update --> " + FleSource + " -> " + FleTarget, "");
                }
                Print2DEL.Close();
                lUpdate = false;
            }
            // ตรวจสอบอีกครั้ง หลังจากโปรแกรมหลักทำงานไม่สำเร็จ สำหรับไฟล์ .Exe  
            Console.WriteLine("");
            string fleSubname;
            for (int i = 0; i < 3; i++)
            {
                fleSubname = "*.exe"; 
                if (i == 1) { fleSubname = "*.txt"; }
                if (i == 2) { fleSubname = "*.ini"; }
                FileList = dFolderUpdate.GetFiles(fleSubname, SearchOption.TopDirectoryOnly);
                foreach (FileInfo cFileList in FileList)
                {
                    FleSource = cFileList.FullName.Trim();
                    FleShortname = cFileList.Name.Trim();

                    if (FleShortname.ToLower() == "uyodbackup.exe")  // มีปัญหาการ Copy ทับตัวมันเอง
                    { FleTarget = FldGdrive + "Backup\\uyodbackup.upd"; }
                    else { FleTarget = FldBDPos + FleShortname; }

                    switch (FleShortname.ToLower())
                    {
                        case ("readme.txt"):
                            Console.WriteLine(".. " + FleShortname + " Is Original ");
                            break;
                        default:
                            try {
                                File.Copy(FleSource, FleTarget, true);
                                Console.WriteLine(".. Copy " + FleSource + " To " + FleTarget);
                                FleSourceMove = FleSource.Substring(0, FleSource.Length - 4) + "." + DateTime.Now.ToString("ddMMyy HHmmss");
                                File.Move(FleSource, FleSourceMove);
                                Console.WriteLine(".. Move " + FleSource + " To " + FleSourceMove);
                                Write4Text("Update --> " + FleSource + " -> " + FleSourceMove, "");
                            }
                            catch { Console.WriteLine("Copy Or Move Error "+ FleSource+" -> "+ FleTarget); }
                            break;
                    }
                }
            }
            if (lDebug) { Special.DEBUG.D(p, " - 06 *FINNISH"); }
        }



        static void rar4udp(string FleRar, string FolderTarget) //ตรวจสอบไฟล์ปรับปรุงต่างๆ จะมีนามสกุล  ???.udp -2
        {
            // unrar x c:\yourfile.rar *.gif c:\extractfolder\
            // x มีโพลเดอร์ด้วย e ไม่มีโพลเดอร์
            string p= "rar4udp";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            if (nForWork == 0) { Console.WriteLine("FleRar : " + FleRar + " ,FolderTarget : " + FolderTarget); Console.ReadLine(); return; }
            if (!File.Exists(FleRar + ".rar") || !Directory.Exists(FolderTarget)) { return; }
            string FleRename = FleRar + "." + DateTime.Now.ToString("ddMMyy HHmmss");
            try
            {
                Process2Arg("unrar", "x -o+ " + (char)34 + FleRar + (char)34 + " " + (char)34 + FolderTarget + (char)34, "");
                File.Move(FleRar + ".rar", FleRename);
            }
            catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Update File Error : " + ex.Message); return; }
            if (lDebug) { Special.DEBUG.D(p, " - 02 *FINNISH"); }
        }


        static void WriteUyodbackup()  // เขียนไฟล์   uyodbackup.ini  
        {
            FldGdrive = "";
            FldBDPos = @MyFolderApp+"\\";
            string p = "WriteUyodbackup";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            if (FldGdrive == "")
            {
                Console.WriteLine("Type Folder Source : " + FldBDPos);
                Console.WriteLine("Type Folder Backup : G:\\My Drive\\");
                Console.Write("Type Company Name  : ");
                cTemp = Console.ReadLine(); cTemp = cTemp.Trim();
                FldGdrive = @"G:\My Drive\";
                TextWriter Print2Txt = new StreamWriter("uyodbackup.ini");
                Print2Txt.WriteLine(FldGdrive.Trim());
                Print2Txt.WriteLine(FldBDPos.Trim());
                Print2Txt.WriteLine("0"); // 3  
                Print2Txt.WriteLine("60");  // 4
                Print2Txt.WriteLine("               ใส่ Flash Drive เช่น G: ");  // 5  FldGdrive.Trim().Substring(0, 2)
                Print2Txt.WriteLine(cTemp);  // 6 Disk สำรองเผือไว้ 
                Print2Txt.WriteLine("");
                Print2Txt.WriteLine("");
                Print2Txt.WriteLine("");
                Print2Txt.WriteLine("");
                Print2Txt.WriteLine("Line 1 : Cloud+\\");
                Print2Txt.WriteLine("Line 2 : Programn+\\");
                Print2Txt.WriteLine("Line 3 : Shutdown 0=No 1=Shutdown 2=Restart ");
                Print2Txt.WriteLine("Line 4 : เวลา วินาที For Shutdown ");
                Print2Txt.WriteLine("Line 5 : Flash Drive +:");
                Print2Txt.WriteLine("ทำงาน 5 บรรทัดแรก เท่านั้น");
                Print2Txt.WriteLine("MyDBFOthertBackup.txt   ชื่อโพลเดอร์ สำรองไฟล์ DBF อื่นๆ");
                Print2Txt.WriteLine("MyEXEOthertBackup.txt   ชื่อโพลเดอร์ สำรองไฟล์ EXE อื่นๆ");
                Print2Txt.WriteLine("MyDoc4Customer.txt   ชื่อโพลเดอร์ สำรองไฟล์ชุด Office ของลูกค้า");
                Print2Txt.WriteLine("RunDosMode.txt	 คำสั่ง DOS หลังจากทำงานเสร็จ");
                Print2Txt.Close();
                if (cTemp != "")
                {
                    cTemp = cTemp.Trim() + "@" + DateTime.Now.ToString("dddd, dd MMMM yyyy") + "";
                    if (Directory.Exists(@"G:\My Drive")) { cTemp = @"G:\My Drive\" + cTemp; }
                    try { File.Copy("uyodbackup.ini", cTemp, true); }
                    catch { Console.WriteLine("Copy Error ,"+ "uyodbackup.ini"+" -> "+ cTemp); }
                }
                if (lDebug) { Special.DEBUG.D(p," - 02"); }
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(""); Console.WriteLine("");
                Console.WriteLine("File Name : uyodbackup.ini");
                Console.WriteLine(FldGdrive);
                Console.WriteLine(FldBDPos);
                Console.WriteLine("0");
                Console.WriteLine("60");
                Console.ForegroundColor = ConsoleColor.White;
            }
            if (lDebug) { Special.DEBUG.D(p, " - 03 *FINNISH"); }
        }


        static void CopyFleIntxt()  // ไฟล์ใน copyfleintxt.txt 
        {
            string FleInText = FldGdrive + "Uyodteam\\copyfleintxt.txt";
            string p = "CopyFleIntxt";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            UyodMess(FleInText);
            if (!File.Exists(FleInText))
            {
                TextWriter Print2Txt = new StreamWriter(FleInText);
                Print2Txt.WriteLine("");
                Print2Txt.Close();
            }
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            string[] readText = File.ReadAllLines(FleInText);
            foreach (string Fle4Copy in readText)
            {
                if (File.Exists(Fle4Copy))
                {
                    cTemp = Fle4Copy.ToString();
                    if (cTemp.Contains(",")) { Console.WriteLine(cTemp); }
                }
            }
            if (lDebug) { Special.DEBUG.D(p, " - 03 *FINNISH"); }
        }


        static void DelFleIntxt()  // ลบไฟล์ใน delfleintxt.txt
        {
            string FleInText = FldGdrive + "Uyodteam\\delfleintxt.txt";
            string p = "DelFleIntxt";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            if (!File.Exists(FleInText))
            {
                TextWriter Print2Txt = new StreamWriter(FleInText);
                Print2Txt.WriteLine("");
                Print2Txt.Close();
            }
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            string[] readText = File.ReadAllLines(FleInText);
            foreach (string Fle4Del in readText)
            {
                if (File.Exists(Fle4Del)) { File.Delete(Fle4Del); }
            }
            if (lDebug) { Special.DEBUG.D(p, " - 03 *FINNISH"); }
        }


        static void DownloadFromWeb(string FleOnWEB, string FleDownload)
        {
            string p = "DownloadFromWeb";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            FleDownload = Path.GetDirectoryName(MyApp) + "\\" + FleDownload;
            if (File.Exists(FleDownload)) { return; }
            try
            {
                WebClient myWebClient = new WebClient();
                myWebClient.DownloadFile(FleOnWEB, FleDownload);
                myWebClient.Dispose();
            }
            catch (Exception ex)
            {
                Util.Program.Pok_Error(2, cFle4Error, ex.Message);
                Util.Program.Pok_Error(2, cFle4Error, "Download Form  " + FleOnWEB + " To " + FleDownload);
                return;
            }
            if (lDebug) { Special.DEBUG.D(p, " - 02 *FINNISH"); }
        }


        static void BdPos2JuckFile() // ลบไฟล์ไม่ได้ใช้งานของเครื่องที่บ้าน 
        {
            // ลบไฟล์ในโพลเดอร์ 
            string p= "BdPos2JuckFile";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            BdPos4JuckFile(@"C:\Pos\Prog\Ho");
            BdPos4JuckFile(@"C:\Pos\Prog\Poswin");
            BdPos4JuckFile(@"C:\Pos\Security\Prog");
            BdPos4JuckFile(@"C:\Pos\Prog\AP");
            BdPos4JuckFile(@"C:\Pos\Prog\AR");
            BdPos4JuckFile(@"C:\Pos\Prog\BILL");
            BdPos4JuckFile(@"C:\Pos\Prog\CQ");
            BdPos4JuckFile(@"C:\Pos\Prog\CU");
            BdPos4JuckFile(@"C:\Pos\Prog\FORMAIL");
            BdPos4JuckFile(@"C:\Pos\Prog\GL");
            BdPos4JuckFile(@"C:\Pos\Prog\GS");
            BdPos4JuckFile(@"C:\Pos\Prog\IV");
            BdPos4JuckFile(@"C:\Pos\Prog\LS");
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            BdPos4JuckFile(@"C:\Pos\Prog\MA");
            BdPos4JuckFile(@"C:\Pos\Prog\MB");
            BdPos4JuckFile(@"C:\Pos\Prog\PE");
            BdPos4JuckFile(@"C:\Pos\Prog\PO");
            BdPos4JuckFile(@"C:\Pos\Prog\PR");
            BdPos4JuckFile(@"C:\Pos\Prog\VAT");
            BdPos4JuckFile(@"P:\Pos\Prog\Ho");
            BdPos4JuckFile(@"P:\Pos\Prog\Poswin");
            BdPos4JuckFile(@"P:\Pos\Security\Prog");
            if (lDebug) { Special.DEBUG.D(p," - 03"); }
            BdPos4JuckFile(@"P:\Pos\Prog\AP");
            BdPos4JuckFile(@"P:\Pos\Prog\AR");
            BdPos4JuckFile(@"P:\Pos\Prog\BILL");
            BdPos4JuckFile(@"P:\Pos\Prog\CQ");
            BdPos4JuckFile(@"P:\Pos\Prog\CU");
            BdPos4JuckFile(@"P:\Pos\Prog\FORMAIL");
            BdPos4JuckFile(@"P:\Pos\Prog\GL");
            BdPos4JuckFile(@"P:\Pos\Prog\GS");
            BdPos4JuckFile(@"P:\Pos\Prog\IV");
            BdPos4JuckFile(@"P:\Pos\Prog\LS");
            BdPos4JuckFile(@"P:\Pos\Prog\MA");
            if (lDebug) { Special.DEBUG.D(p," - 04"); }
            BdPos4JuckFile(@"P:\Pos\Prog\MB");
            BdPos4JuckFile(@"P:\Pos\Prog\PE");
            BdPos4JuckFile(@"P:\Pos\Prog\PO");
            BdPos4JuckFile(@"P:\Pos\Prog\PR");
            BdPos4JuckFile(@"P:\Pos\Prog\VAT");
            BdPos4JuckFile(@"D:\BdPos");
            BdPos4JuckFile(@"D:\BdPosVFP");
            BdPos4JuckFile(@"D:\SampleVFP");
            // BdPos4JuckFile(@"E:\Temp\_1");
            if (lDebug) { Special.DEBUG.D(p," - 05"); }
            nPress = 0;
            // ลบไฟล์บางนามสุกลทุกโพลเดอร์
            BdPos4JuckFolder(@"C:\Pos\Prog");
            BdPos4JuckFolder(@"P:\Pos\Prog");
            BdPos4JuckFolder(@"R:\Drvd");
            BdPos4JuckFolder(@"D:\BdPos");
            BdPos4JuckFolder(@"D:\BdPosVFP");
            BdPos4JuckFolder(@"D:\SampleVFP");
            if (lDebug) { Special.DEBUG.D(p," - 06"); }
        }


        static void BdPos4JuckFolder(string cFoder4DEL) // ลบไฟล์บางนามสุกลทุกโพลเดอร์
        {
            string p = "BdPos4JuckFolder";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            if (!Directory.Exists(cFoder4DEL)) { return; }
            Console.WriteLine("");
            Console.WriteLine("Folder : " + cFoder4DEL);
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            try
            {
                DirectoryInfo dFolderProg = new DirectoryInfo(cFoder4DEL);
                FileInfo[] FileList = dFolderProg.GetFiles("*.bak", SearchOption.AllDirectories);
                foreach (FileInfo cFileList in FileList)
                {
                    Console.WriteLine("Delete File : " + cFileList.FullName.ToString());
                    if (nPress != 9) { pressKeyBoard("Press D For Delete,Press A For Delete All"); }
                    if (nPress == 1 || nPress == 2 || nPress == 9) { File.Delete(cFileList.FullName); }
                }
            }
            catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Delete File Error : " + ex.Message); }
            if (lDebug) { Special.DEBUG.D(p," - 03"); }
            nPress = 0;
            try
            {
                DirectoryInfo dFolderProg = new DirectoryInfo(cFoder4DEL);
                FileInfo[] FileList = dFolderProg.GetFiles("*.tmp", SearchOption.AllDirectories);
                foreach (FileInfo cFileList in FileList)
                {
                    Console.WriteLine("Delete File : " + cFileList.FullName.ToString());
                    if (nPress != 9) { pressKeyBoard("Press D For Delete,Press A For Delete All"); }
                    if (nPress == 1 || nPress == 2 || nPress == 9) { File.Delete(cFileList.FullName); }
                }
            }
            catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Delete File Error : " + ex.Message); }
            nPress = 0;
            if (lDebug) { Special.DEBUG.D(p, " - 04 *FINNISH"); }
        }


        static void BdPos4JuckFile(string cFoder4DEL) // ลบไฟล์ในเครื่องเขียนโปรแกรม-2
        {
            string p = "BdPos4JuckFile";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            if (!Directory.Exists(cFoder4DEL)) { return; }
            Console.WriteLine("");
            Console.WriteLine("Folder : " + cFoder4DEL);
            if (Directory.Exists(cFoder4DEL + "\\_Error")) { BdPos5JuckFile(cFoder4DEL + "\\_Error"); }
            if (Directory.Exists(cFoder4DEL + "\\BakData")) { BdPos5JuckFile(cFoder4DEL + "\\BakData"); }
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            if (Directory.Exists(cFoder4DEL + "\\BrData")) { BdPos5JuckFile(cFoder4DEL + "\\BrData"); }
            if (Directory.Exists(cFoder4DEL + "\\Datatemp")) { BdPos5JuckFile(cFoder4DEL + "\\Datatemp"); }
            if (Directory.Exists(cFoder4DEL + "\\DayInOut")) { BdPos5JuckFile(cFoder4DEL + "\\DayInOut"); }
            if (Directory.Exists(cFoder4DEL + "\\Error")) { BdPos5JuckFile(cFoder4DEL + "\\Error"); }
            if (Directory.Exists(cFoder4DEL + "\\Temp")) { BdPos5JuckFile(cFoder4DEL + "\\Temp"); }
            if (Directory.Exists(cFoder4DEL + "\\Txt")) { BdPos5JuckFile(cFoder4DEL + "\\Txt"); }
            if (Directory.Exists(cFoder4DEL + "\\TxtRepo")) { BdPos5JuckFile(cFoder4DEL + "\\TxtRepo"); }
            if (Directory.Exists(cFoder4DEL + "\\UserPG")) { BdPos5JuckFile(cFoder4DEL + "\\UserPG"); }
            nPress = 0;
            if (lDebug) { Special.DEBUG.D(p, " - 03 *FINNISH"); }
        }

        static void BdPos5JuckFile(string cFoder5DEL) // ลบไฟล์ในเครื่องเขียนโปรแกรม-3 
        {
            string p = "BdPos5JuckFile";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            try
            {
                DirectoryInfo dFolderProg = new DirectoryInfo(cFoder5DEL);
                FileInfo[] FileList = dFolderProg.GetFiles("*.*", SearchOption.TopDirectoryOnly);
                foreach (FileInfo cFileList in FileList)
                {
                    if (ChkFle2DEL(cFileList.Name))
                    {
                        Console.WriteLine("Delete File : " + cFileList.FullName.ToString());
                        if (nPress != 9) { pressKeyBoard("Press D For Delete,Press A For Delete All"); }
                        if (nPress == 1 || nPress == 2 || nPress == 9) { File.Delete(cFileList.FullName); }
                    }
                }
            }
            catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Delete File Error : " + ex.Message); return; }
            if (lDebug) { Special.DEBUG.D(p, " - 02 *FINNISH"); }
        }


        // สำหรับรับค่า D,Y,A
        static void pressKeyBoard(string cSayText)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(cSayText);
            Console.ForegroundColor = ConsoleColor.White;
            string cYesno = Console.ReadLine();
            if (cYesno == null || cYesno == "")
                nPress = 0;
            else
            {
                cYesno = cYesno.ToUpper().Trim().Substring(0, 1);
                switch (cYesno)
                {
                    case ("Y"):
                    case ("ั"):
                    case ("ํ"):
                        nPress = 1;
                        break;
                    case ("D"):
                    case ("ฏ"):
                        nPress = 2;
                        break;
                    case ("A"):
                    case ("ฤ"):
                        nPress = 9;
                        break;
                }
            }
        }

        static void DeleteSourceBackup() // ถ้าเป็นวันที่ 1 ให้ลบไฟล์ Backup เพื่อสร้างใหม่อีกรอบ 
        {
            string p = "DeleteSourceBackup";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            DirectoryInfo dFolderProg = new DirectoryInfo(cTemp);
            FileInfo[] FileList = dFolderProg.GetFiles("*.rar", SearchOption.AllDirectories);
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            foreach (FileInfo cFileList in FileList)
            { File.Delete(cFileList.FullName); }
            if (lDebug) { Special.DEBUG.D(p, " - 03 *FINNISH"); }
            return;
        }


        static void SourceCodeBackup(int noption) // สำรอง SourceCode ไป R:\BdBackup  
        {
            string p = "SourceCodeBackup";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            if (!Directory.Exists(@"R:")) { UyodMess("ไม่พบไดร์ R:"); return; } // ไม่เจอ Drive R ให้ออกไป 
            Layout.Program.LayOut.BackGroud(3);
             if (noption==1)    
            {
                OpenAppCloud(1); // สำหรับเปิดโปรแกรมคลาวด์ 
                if (nForWork == 1) { Thread.Sleep(90 * 1000); } // รอ Gdrive เปิดให้สมบูรณ์ ป้องกัน error ตอนเขียนไฟล์ 
            }

            cTemp = @"R:\BDBackup";
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            if (!Directory.Exists(cTemp)) { Directory.CreateDirectory(cTemp); UyodMess("สร้างโพลเดอร์ " + cTemp); }
            if (DateTime.Now.ToString("d") == "1") { DeleteSourceBackup(); }  // ถ้าเป็นวันที่ 1 ให้ลบไฟล์ Backup เพื่อสร้างใหม่อีกรอบ 
            if (lDebug) { Special.DEBUG.D(p," - 03"); }
            cTemp = cTemp + "\\";
            Source2Backup(@"\Pos\Security\Prog\Alinit\Master", cTemp + "masorg", "");
            Source2Backup(@"\Pos\Security\Prog\Alinit\Transac", cTemp + "tranorg", "");
            Source2Backup(@"\Pos\Util", cTemp + "util", "");
            Source2Backup(@"\Pos\Company", cTemp + "company", "");
            Source2Backup(@"\Pos\Environment", cTemp + "Environment", "");
            if (lDebug) { Special.DEBUG.D(p," - 04"); }
            Source2Backup(@"\Pos\Prog\Iv", cTemp + "iv", "iv");
            Source2Backup(@"\Pos\Prog\Po", cTemp + "po", "po");
            Source2Backup(@"\Pos\Prog\Ap", cTemp + "ap", "ap");
            Source2Backup(@"\Pos\Prog\Ar", cTemp + "ar", "ar");
            Source2Backup(@"\Pos\Prog\Gl", cTemp + "gl", "gl");
            if (lDebug) { Special.DEBUG.D(p," - 05"); }
            Source2Backup(@"\Pos\Prog\Formail", cTemp + "formail", "formail");
            Source2Backup(@"\Pos\Prog\Bill", cTemp + "bill", "bill");
            Source2Backup(@"\Pos\Prog\Ls", cTemp + "ls", "ls");
            Source2Backup(@"\Pos\Prog\Pe", cTemp + "pe", "pe");
            Source2Backup(@"\Pos\Prog\Ma", cTemp + "ma", "ma");
            Source2Backup(@"\Pos\Security\Prog", cTemp + "sc", "sc");
            if (lDebug) { Special.DEBUG.D(p," - 06"); }
            Source2Backup(@"\Pos\Prog\Ho", cTemp + "ho", "ho");
            Source2Backup(@"\Pos\Prog\Poswin", cTemp + "poswin", "poswin");
            Source2Backup(@"\Pos\Data", cTemp + "data", "");
            Source2Backup(@"\Pos\Data\Transac", cTemp + "transac", "");
            if (lDebug) { Special.DEBUG.D(p," - 07"); }
            try
            {
                // เครื่องมือต่างๆ  
                if (Directory.Exists(SourceMyGdrive+"Document"))
                {
                    ChageFolder(SourceMyGdrive+"Document");
                    Process2Arg("Rar", "u -r " + (char)34 + cTemp + "MyDocument" + (char)34 + " *.pmd *.bmp *.doc *.s2g *.pdf *.vsd *.bat", "");
                }
                // HTML
                if (Directory.Exists(SourceMyGdrive+"Internet"))
                {
                    ChageFolder(SourceMyGdrive + "Internet");
                    Process2Arg("Rar", "u -r " + (char)34 + cTemp + "MyInternet" + (char)34 + " *.html *.js *.php", "");
                }
                if (lDebug) { Special.DEBUG.D(p," - 08"); }
                // สำรอง C# 
                if (Directory.Exists(Source4Gdrive + @"MEGAsync\C#"))
                {
                    ChageFolder(Source4Gdrive + @"MEGAsync\C#");
                    Process2Arg("Rar", "u -r " + (char)34 + cTemp + "MyC" + (char)34 + " *.cs *.ico *.bat *.txt", "");
                }
                // Foxuser
                if (Directory.Exists(@"C:\POS\FPW26"))
                {
                    ChageFolder(@"C:\POS\FPW26");
                    Process2Arg("Rar", "u -r " + (char)34 + cTemp + "Foxuser_c" + (char)34 + " FOXUSER.*", "");
                }
                if (lDebug) { Special.DEBUG.D(p," - 09"); }
                if (Directory.Exists(@"P:\Pos\VFP6"))
                {
                    ChageFolder(@"P:\Pos\VFP6");
                    Process2Arg("Rar", "u -r " + (char)34 + cTemp + "Foxuser_p" + (char)34 + " FOXUSER.*", "");
                }
                // _Update
                if (Directory.Exists(@"R:\DRVD\BdPosVFP\_update"))
                {
                    if (File.Exists(cTemp + "bdpos_updt_drvR.rar")) { File.Delete(cTemp + "bdpos_updt_drvR.rar"); }
                    ChageFolder(@"R:\DRVD\BdPosVFP\_update");
                    Process2Arg("Rar", "u " + (char)34 + cTemp + "bdpos_updt_drvR" + (char)34 + " *.*", "");
                }
                if (lDebug) { Special.DEBUG.D(p," - 10"); }
                // Temp 
                if (Directory.Exists(@"R:\Temp"))
                {
                    if (File.Exists(cTemp + "temp_in_drvR.rar")) { File.Delete(cTemp + "temp_in_drvR.rar"); }
                    ChageFolder(@"R:\Temp");
                    Process2Arg("Rar", "u " + (char)34 + cTemp + "temp_in_drvR" + (char)34 + " *.prg *.txt *.doc *.xls *.sc? *.fr?", "");
                }
                if (lDebug) { Special.DEBUG.D(p," - 11"); }
                if (Directory.Exists(@MyFolderApp+"\\Customer"))
                {
                    ChageFolder(@MyFolderApp+"\\Customer");
                    Process2Arg("Rar", "u -r " + (char)34 + cTemp + "customer_drvD" + (char)34 + " *.*", "");
                }
            }
            catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Error : " + ex.Message); }
            if (lDebug) { Special.DEBUG.D(p," - 12"); }
            ReadOnLyFleBack(); // อ่านเฉยๆเพื่อวัน/เวลาอัพไฟล์ 

            // Copy ไฟโพลเดอร์อื่นๆของคลาวด์
            if (lDebug) { Special.DEBUG.D(p," - 13"); }
            DirectoryInfo dFolderZip = new DirectoryInfo(cTemp);
            FileInfo[] FileZip = dFolderZip.GetFiles("*.*", SearchOption.TopDirectoryOnly);
            Console.WriteLine(""); Console.WriteLine("");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Wait For Backup To Cloud");
            if (lDebug) { Special.DEBUG.D(p," - 14"); }
            if (noption == 1) { if (nForWork == 1) { Thread.Sleep(20 * 1000); } }

            Console.ForegroundColor = ConsoleColor.White;
            foreach (FileInfo cFileList in FileZip)  // สำรองในแต่ละคลาวด์
            {
                for (int i = 1; i <= 6; i++)
                {
                    switch (i)
                    {
                        case (1):
                            cTemp2 = @"R:\BdBackup"; break;
                        case (2):
                            cTemp2 = Source4Gdrive + @"OneDrive\Source"; break;
                        case (3):
                            cTemp2 = SourceMyGdrive+"Source"; break;
                        case (4):
                            cTemp2 = Source4Gdrive + @"MEGAsync\Source"; break;
                        case (5):
                            cTemp2 = Source4Gdrive + @"Box Sync\Source"; break;
                        case (6):
                            cTemp2 = @"E:\Temp\_3"; break;  // ทดสอบ
                    }
                    if (lDebug) { Special.DEBUG.D(p," - 15"); }
                    Console.WriteLine("Folder = " + cTemp2);
                    if (Directory.Exists(cTemp2))
                    {
                        cTemp2 = cTemp2 + @"\" + DateTime.Now.ToString("ddd");
                        if (!Directory.Exists(cTemp2)) { Directory.CreateDirectory(cTemp2); }
                        cTemp2 = cTemp2 + @"\";
                            // เปรียบเทียบก่อน Copy ว่าใหม่กว่ามัย 
                            if (CompareFile(cFileList.FullName, cTemp2 + cFileList))
                            {
                                try  {
                                File.Copy(cFileList.FullName, cTemp2 + cFileList, true);
                                Console.WriteLine("==> Copy File " + cFileList.Name
                                   + " , DATE : " + cFileList.LastWriteTime.ToString("dd/MM/yyyy HH:mm:ss")
                                   + " , SIZE : " + (cFileList.Length / 1048576).ToString() + " MB.");
                            }
                            catch { Console.WriteLine("Copy Error "+ cFileList.FullName+" --> "+ cTemp2 + cFileList); }
                            }
                            else // ไม่ต้อง Copy 
                            {
                                Console.WriteLine("==> Don't Copy File " + cFileList.Name
                                   + " , DATE : " + cFileList.LastWriteTime.ToString("dd/MM/yyyy HH:mm:ss")
                                   + " , SIZE : " + (cFileList.Length / 1048576).ToString() + " MB.");
                            }
                    }
                }
            }
            if (lDebug) { Special.DEBUG.D(p," - 16"); }
            // ไฟล์ช่วยทำงานระบบคุมไฟ  
            Del4FLEInDay("P:\\Temp", 7, "tCard*.txt");
            Del4FLEInDay("R:\\Temp", 7, "tCard*.txt");
            Del4FLEInDay("P:\\Pos\\Prog\\Ho\\History", 7, "Open*.txt");
            if (lDebug) { Special.DEBUG.D(p," - 17"); }
            Console.WriteLine(""); Console.WriteLine("");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.ForegroundColor = ConsoleColor.White;
            // if (nForWork == 1) { Thread.Sleep(20 * 1000); }
            if (noption == 1) { OpenAppCloud(2); }  // สำหรับเปิดโปรแกรม FileZilla
            if (lDebug) { Special.DEBUG.D(p, " - 18 *FINNISH"); }
        }

        static void SourceCode2FlashDrive() // สำรอง SourceCode ไป Flash Drive 
        {
            string p = "SourceCode2FlashDrive";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            cTemp = DiskBackup(1, 2);  // หา Harddisk แบบ Remove ก่อน Label REMOVEABLE
            if (cTemp == "")
            {
                cTemp = DiskBackup(2, 2);  // หาต่อที่เป็น Flash Drive 
                if (cTemp == "")
                {
                    Console.WriteLine("No Found Flash Drive " + cTemp);
                    Util.Program.Pok_Mess("ไม่พบ Flash Drive"); return;
                }
            }
            // เริ่มทำงาน 
            Layout.Program.LayOut.BackGroud(3);
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            Console.WriteLine("Found Flash Drive " + cTemp);
            if (Directory.Exists("C:\\POS"))
            {
                Directory.SetCurrentDirectory("C:\\Pos");
                cTemp2 = cTemp + "POS_C_" + DateTime.Now.ToString("ddd") + " *.bat *.prg *.sc? *.fr? *.pj?";
                Console.WriteLine("Found C:\\POS");
                Console.WriteLine("rar " + "u -r " + cTemp2);
                Util.Program.Pok_Process("rar", "u -r " + cTemp2);
            }
            if (lDebug) { Special.DEBUG.D(p," - 03"); }
            if (Directory.Exists("P:\\POS"))
            {
                Directory.SetCurrentDirectory("P:\\Pos");
                cTemp2 = cTemp + "POS_P_" + DateTime.Now.ToString("ddd") + " *.bat *.prg *.sc? *.fr? *.pj?";
                Console.WriteLine("Found P:\\POS");
                Console.WriteLine("rar " + "u -r " + cTemp2);
                Util.Program.Pok_Process("rar", "u -r " + cTemp2);
            }
            if (lDebug) { Special.DEBUG.D(p," - 04"); }
            Util.Program.Pok_Process("explorer", cTemp);
            Console.WriteLine("Backup Complete");
            cTemp = ""; cTemp2 = "";
            if (lDebug) { Special.DEBUG.D(p," - 05"); }
        }

        static void OpenAppCloud(int nOption)
        {
            string p = "OpenAppCloud";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            try
            {
                if (File.Exists(@"C:\Program Files\Box\Box Sync\BoxSync.exe") && nOption == 1)
                {
                    Console.WriteLine("");
                    Console.WriteLine("Open Box.Com");
                    ChageFolder(@"C:\Program Files\Box\Box Sync");
                    if (nForWork == 1) { Process.Start("BoxSync.exe"); } else { Console.WriteLine("Process.Start -->Run BoxSync.exe"); Console.ReadLine(); }
                    if (nForWork == 1) { Thread.Sleep(2 * 1000); }
                }
                if (lDebug) { Special.DEBUG.D(p," - 01"); }
                if (File.Exists("Norun.Fle") && nOption == 1)
                {
                    // No
                    // MEGA 
                    cTemp = @"C:\Users\" + Environment.UserName.Trim() + @"\AppData\Local\MEGAsync";
                    cTemp2 = cTemp + @"\MEGAsync.exe";
                    if (File.Exists(cTemp2))
                    {
                        Console.WriteLine("");
                        Console.WriteLine("Open MEGA");
                        ChageFolder(cTemp);
                        if (nForWork == 1) { Process.Start("MEGAsync.exe"); Thread.Sleep(2 * 1000); }
                        else { Console.WriteLine("Process.Start -->Run MEGAsync.exe"); Console.ReadLine(); }
                    }
                    // ONEDRIVE
                    cTemp = @"C:\Users\" + Environment.UserName.Trim() + @"\AppData\Local\Microsoft\OneDrive";
                    cTemp2 = cTemp + @"\OneDrive.exe";
                    if (lDebug) { Special.DEBUG.D(p," - 03"); }
                    if (File.Exists(cTemp2))
                    {
                        Console.WriteLine("");
                        Console.WriteLine("Open ONEDRIVE");
                        ChageFolder(cTemp);
                        if (nForWork == 1) { Process.Start("OneDrive.exe"); Thread.Sleep(2 * 1000); }
                        else { Console.WriteLine("Process.Start -->Run OneDrive.exe"); Console.ReadLine(); }
                    }
                    cTemp = @"C:\Program Files\Google\Drive File Stream";
                    if (lDebug) { Special.DEBUG.D(p," - 04"); }
                    if (Directory.Exists(cTemp) && nOption == 1)
                    {
                        DirectoryInfo dFolderProg = new DirectoryInfo(cTemp);
                        FileInfo[] FileList = dFolderProg.GetFiles("GoogleDriveFS.exe", SearchOption.AllDirectories);
                        cTemp2 = "";
                        foreach (FileInfo cFileList in FileList)
                        {
                            cTemp2 = cFileList.DirectoryName;
                        }
                        if (cTemp2 != "")
                        {
                            Console.WriteLine("");
                            Console.WriteLine("Open Google Drive");
                            ChageFolder(cTemp2);
                            Console.WriteLine(cTemp);
                            if (nForWork == 1) { Process.Start("GoogleDriveFS.exe"); Thread.Sleep(2 * 1000); }
                            else { Console.WriteLine("Process.Start -->Run GoogleDriveFS.exe In " + cTemp2); Console.ReadLine(); }
                        }
                    }
                }
                if (lDebug) { Special.DEBUG.D(p," - 05"); }
                if (File.Exists(@"C:\Program Files (x86)\FileZilla FTP Client\filezilla.exe") && nOption == 2)
                {
                    Console.WriteLine("");
                    Console.WriteLine("Open FileZilla");
                    ChageFolder(@"C:\Program Files (x86)\FileZilla FTP Client");
                    if (nForWork == 1) { Process.Start("filezilla.exe"); Thread.Sleep(2 * 1000); }
                    else { Console.WriteLine("Process.Start -->Run filezilla.exe"); Console.ReadLine(); }
                }
                if (lDebug) { Special.DEBUG.D(p," - 06"); }
                if (File.Exists(@"C:\Program Files\FileZilla FTP Client\filezilla.exe") && nOption == 2)
                {
                    Console.WriteLine("");
                    Console.WriteLine("Open FileZilla");
                    ChageFolder(@"C:\Program Files\FileZilla FTP Client");
                    if (nForWork == 1) { Process.Start("filezilla.exe"); Thread.Sleep(2 * 1000); }
                    else { Console.WriteLine("Process.Start -->Run filezilla.exe"); Console.ReadLine(); }
                }
            }
            catch (Exception ex) { Util.Program.Pok_Error(2, cFle4Error, "Error : " + ex.Message); }
            Console.ForegroundColor = ConsoleColor.White;
            if (lDebug) { Special.DEBUG.D(p, " - 07 *FINNISH"); }
        }


        static bool CompareFile(string FLESouce, string FLETarget) // เปรียบเทียบไฟล์และCopy 
        {
            bool lCompareFile = false;
            string SoureDRV = FldBDPos.ToUpper().Substring(3, 5);
            if (File.Exists(FLESouce) && !File.Exists(FLETarget)) { return true; } // ไม่เจอไฟล์ให้ทำงานเลย 
            if (File.GetLastWriteTime(FLESouce) > File.GetLastWriteTime(FLETarget)) { lCompareFile = true; }
            return lCompareFile;
        }


        static void ReadOnLyFleBack() // อ่านเฉยๆเพื่อวัน/เวลาอัพไฟล์
        {
            DirectoryInfo dFolderBak;
            dFolderBak = new DirectoryInfo(FldGdrive);
            FileInfo[] FileBD = dFolderBak.GetFiles("*.rar", SearchOption.AllDirectories);
            foreach (FileInfo cFileList in FileBD)
            {
                Console.WriteLine("READ : " + cFileList.FullName);
            }
            if (Directory.Exists(@"R:\BdBackup"))
            {
                dFolderBak = new DirectoryInfo(@"R:\BdBackup");
                FileInfo[] FileBak = dFolderBak.GetFiles("*.rar", SearchOption.AllDirectories);
                foreach (FileInfo cFileList in FileBak)
                {
                    Console.WriteLine("READ : " + cFileList.FullName);
                }
            }
            if (Directory.Exists(Source4Gdrive))
            {
                dFolderBak = new DirectoryInfo(Source4Gdrive);
                FileInfo[] FileBak2 = dFolderBak.GetFiles("*.rar", SearchOption.AllDirectories);
                foreach (FileInfo cFileList in FileBak2)
                {
                    Console.WriteLine("READ : " + cFileList.FullName);
                }
            }
            if (Directory.Exists(@"E:\Temp\_3"))
            {
                dFolderBak = new DirectoryInfo(@"E:\Temp\_3");
                FileInfo[] FileBak3 = dFolderBak.GetFiles("*.rar", SearchOption.AllDirectories);
                foreach (FileInfo cFileList in FileBak3)
                {
                    Console.WriteLine("READ : " + cFileList.FullName);
                }
            }
        }

        static void DelLunkJPG(string cFolder)
        {
            if (!Directory.Exists(cFolder)) { return; }
            string Fle4Del = "";
            for (int i = 1; i <= 4; i++)  // สำรองในแต่ละ Drive 
            {
                switch (i)
                {
                    case (1): Fle4Del = cFolder + "\\Temp"; break;
                    case (2): Fle4Del = cFolder + "\\Error"; break;
                    case (3): Fle4Del = cFolder + "\\_Error"; break;
                    case (4): Fle4Del = cFolder + "\\Room"; break;
                }
                if (Directory.Exists(Fle4Del)) // ลบใน TEMP
                {
                    DirectoryInfo dFolderProg = new DirectoryInfo(Fle4Del);
                    FileInfo[] FileList = dFolderProg.GetFiles("*.jpg", SearchOption.AllDirectories);
                    foreach (FileInfo cFileList in FileList)
                    {
                        File.Delete(cFileList.FullName);
                        Console.WriteLine("Del : " + cFileList.FullName);
                    }
                }
            }
        }



        static void Source2Backup(string FolderSource, string FleTarget, string cSysDIV) // สำรอง SourceCode ไป R:\BdBackup-2
        {
            string p = "Source2Backup";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            FolderSource = FolderSource.Trim(); FleTarget = FleTarget.Trim();
            DelLunkJPG(FolderSource);   // ลบไฟล์ขยะในโพลเดอร์ Temp และ Error  
            string cDrvWork, cDrvWorkSubfix, cDrvDbfrun, cDrvInit;
            cDrvWork = ""; cDrvWorkSubfix = cDrvWork;
            cDrvDbfrun = FolderSource + @"\Dbfrun";
            cDrvInit = FolderSource + @"\Init";
            string ExpFle = "*.exe *.prg *.mn? *.sc? *.fr? *.txt *.?at *.bmp *.gif *.pj? *.dbf *.cdx *.org *.doc *.pmd *.fpw *.jpg *.png *.ico *.fpt";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            for (int i = 1; i <= 2; i++)  // สำรองในแต่ละ Drive 
            {
                switch (i)
                {
                    case (1): cDrvWork = "C:"; cDrvWorkSubfix = "_c"; break;
                    case (2): cDrvWork = "P:"; cDrvWorkSubfix = "_p"; break;
                }
                if (Directory.Exists(cDrvWork + FolderSource))
                {
                    Console.WriteLine("");
                    Console.WriteLine("Folder :" + cDrvWork + FolderSource);
                    ChageFolder(cDrvWork + FolderSource);
                    Process2Arg("Rar", "u " + (char)34 + FleTarget + cDrvWorkSubfix + (char)34 + " " + ExpFle, "");  // สำรองเฉพาะ โพลเดอร์โปรแกรม
                    if ((cSysDIV == "XXXXXXX"))  //  (cSysDIV != "")    ทำการแยกสำรองอีกครั้ง เผื่อมีปัญหาไฟล์หลักพัง 
                    {
                        string cTargetDIV, ExpFleDIV, NameFleDIV;
                        for (int idiv = 1; i <= 11; i++)
                        {
                            cTargetDIV = FleTarget + "SysProg\\" + cSysDIV;
                            if (!Directory.Exists(cTargetDIV)) { Directory.CreateDirectory(cTargetDIV); }
                            ExpFleDIV = ""; NameFleDIV = "";
                            switch (idiv)
                            {
                                case (1): ExpFleDIV = "*.exe *.txt"; NameFleDIV = "exe"; break;
                                case (2): ExpFleDIV = "*.prg *.txt *.?at"; NameFleDIV = "prgtxtbat"; break;
                                case (3): ExpFleDIV = "*.mn?"; NameFleDIV = "menu"; break;
                                case (4): ExpFleDIV = "*.sc?"; NameFleDIV = "screen"; break;
                                case (5): ExpFleDIV = "*.fr?"; NameFleDIV = "report"; break;
                                case (6): ExpFleDIV = "*.bmp *.gif *.jpg *.png *.ico"; NameFleDIV = "picture"; break;
                                case (7): ExpFleDIV = "*.pj?"; NameFleDIV = "project"; break;
                                case (8): ExpFleDIV = "*.dbf *.cdx"; NameFleDIV = "dbf"; break;
                                case (9): ExpFleDIV = "*.org"; NameFleDIV = "org"; break;
                                case (10): ExpFleDIV = "*.doc *.pmd"; NameFleDIV = "docpmd"; break;
                                case (11): ExpFleDIV = "*.fpw"; NameFleDIV = "fpw"; break;
                            }
                            if (ExpFleDIV != "")
                            {
                                NameFleDIV = "\\" + NameFleDIV.Trim();
                                Console.WriteLine("Folder :" + cTargetDIV + NameFleDIV + " ,For " + ExpFleDIV);
                                Process2Arg("Rar", "u " + (char)34 + cTargetDIV + NameFleDIV + (char)34 + " " + ExpFleDIV, "");
                            }
                        }
                    }
                }
                if (lDebug) { Special.DEBUG.D(p," - 02"); }
                // ตรวจสอบโพลเดอร์ Dbfrun
                if (Directory.Exists(cDrvWork + cDrvDbfrun))
                {
                    Console.WriteLine("");
                    Console.WriteLine("Folder :" + cDrvWork + cDrvDbfrun);
                    ChageFolder(cDrvWork + cDrvDbfrun);
                    Process2Arg("Rar", "u " + (char)34 + FleTarget + "_dbfrun" + cDrvWorkSubfix + (char)34 + " " + ExpFle, "");
                    // Console.ReadLine();
                }
                if (lDebug) { Special.DEBUG.D(p," - 01"); }
                // ตรวจสอบโพลเดอร์ Init  เช่น  P:\Pos\PROG\HO\Init
                if (Directory.Exists(cDrvWork + cDrvInit))
                {
                    Console.WriteLine("");
                    Console.WriteLine("Folder :" + cDrvWork + cDrvInit);
                    if (Directory.Exists(cDrvWork + cDrvInit))
                    {
                        ChageFolder(cDrvWork + cDrvInit);
                        Process2Arg("Rar", "u " + (char)34 + FleTarget + "_init" + cDrvWorkSubfix + (char)34 + " " + ExpFle, "");
                    }
                }
            }
            if (lDebug) { Special.DEBUG.D(p, " - 04 *FINNISH"); }
        } 


        static void MyDoc4Customer() // สำรองเอกสารต่างๆของลูกค้า 
        {
            string p = "MyDoc4Customer";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            string FleDer4Cus = FldGdrive + "My Doc";
            if (!Directory.Exists(FleDer4Cus)) { Directory.CreateDirectory(FleDer4Cus); }
            FleDer4Cus = FleDer4Cus + "\\" + Environment.GetEnvironmentVariable("USERNAME");
            if (!Directory.Exists(FleDer4Cus)) { Directory.CreateDirectory(FleDer4Cus); }
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            if (!File.Exists(FldGdrive + "Backup\\" + "MyDoc4Customer.txt"))
            {
                TextWriter Print2Txt = new StreamWriter(FldGdrive + "Backup\\" + "MyDoc4Customer.txt");
                Print2Txt.WriteLine("ใส่โพลเดอร์ที่เก็บเอกสารลูกค้า เช่น C:\\Users\\MANNOWAR\\Documents");
                Print2Txt.Close(); Print2Txt.Dispose();
            }
            if (lDebug) { Special.DEBUG.D(p," - 03"); }
            string[] readtext = File.ReadAllLines(FldGdrive + "Backup\\" + "MyDoc4Customer.txt");
            foreach (string FleDoc in readtext)
            {
                cTemp = FleDoc.Trim();
                if (HaveDbfandExeFile(3, cTemp))
                {
                    ChageFolder(cTemp);
                    Process2Arg("Rar", "u -r " + (char)34 + FleDer4Cus + "MyDoc" + (char)34 + " *.pmd *.doc *.docx *.xls *.xlsx *.gif", "");
                }
            }
            cTemp = ""; cTemp = "";
            if (lDebug) { Special.DEBUG.D(p, " - 04 *FINNISH"); }
        }


        static void MyOtherFolderBackup() // สำรองจากโพลเดอร์อื่นๆ ใน MyDBFOthertBackup.txt
        {
            string p = "MyOtherFolderBackup";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            TextWriter Print2Txt;
            string[] readtext;
            string FleisText;
            // MyDBFOthertBackup.txt  
            FleisText = FldGdrive + "Backup\\" + "MyDBFOthertBackup.txt";
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            if (!File.Exists(FleisText))
            {
                Print2Txt = new StreamWriter(FleisText);
                Print2Txt.WriteLine("ใส่โพลเดอร์ที่ DBF อื่นๆ เช่น 2021");
                Print2Txt.Close(); Print2Txt.Dispose();
            }
            if (lDebug) { Special.DEBUG.D(p," - 03"); }
            readtext = File.ReadAllLines(FleisText);
            foreach (string FleDoc in readtext)
            {
                cTemp = FleDoc.Trim();
                if (HaveDbfandExeFile(1, cTemp))
                {
                    cTemp2 = cTemp;
                    // ตรวจสอบ ความเป็น Folder 
                    if (cTemp2.Substring(1, 2) == @":\") { cTemp2 = cTemp2.Substring(3); }
                    if (cTemp2.Substring(0, 1) == @"\") { cTemp2 = cTemp2.Substring(1); }
                    cTemp2 = FldGdrive + "Backup\\Other\\" + cTemp2;
                    if (!Directory.Exists(cTemp2)) { Directory.CreateDirectory(cTemp2); }

                    ChageFolder(cTemp);
                    Process2Arg("Rar", "u -r " + (char)34 + cTemp2 + (char)34 + " *.dbf *.cdx *.fpt", "");
                }
            }
            if (lDebug) { Special.DEBUG.D(p," - 04"); }
            FleisText = FldGdrive + "Backup\\" + "MyEXEOthertBackup.txt";
            if (!File.Exists(FleisText))
            {
                Print2Txt = new StreamWriter(FleisText);
                Print2Txt.WriteLine("ใส่โพลเดอร์ที่ EXE อื่นๆ เช่น Temp");
                Print2Txt.Close(); Print2Txt.Dispose();
            }
            if (lDebug) { Special.DEBUG.D(p," - 05"); }
            readtext = File.ReadAllLines(FleisText);
            foreach (string FleDoc in readtext)
            {
                cTemp = FleDoc.Trim();
                if (HaveDbfandExeFile(2, cTemp))
                {
                    cTemp2 = cTemp;
                    // ตรวจสอบ ความเป็น Folder 
                    if (cTemp2.Substring(1, 2) == @":\") { cTemp2 = cTemp2.Substring(3); }
                    if (cTemp2.Substring(0, 1) == @"\") { cTemp2 = cTemp2.Substring(1); }
                    cTemp2 = FldGdrive + "Backup\\Other\\" + cTemp2;
                    if (!Directory.Exists(cTemp2)) { Directory.CreateDirectory(cTemp2); }

                    ChageFolder(cTemp);
                    Process2Arg("Rar", "u -r " + (char)34 + cTemp2 + (char)34 + " *.exe", "");
                }
            }
            if (lDebug) { Special.DEBUG.D(p, " - 05 *FINNISH"); }
        }


        static void FileHaveIsHiden()  // ทำไฟล์ FileHave.dir ให้เป็น Hidden 
        {
            Console.WriteLine("");
            FileHaveIs2Hiden("C:\\Pos");
            FileHaveIs2Hiden("G:\\Pos");
            FileHaveIs2Hiden("D:\\BdPos");
            FileHaveIs2Hiden("D:\\BdPosVFP");
            FileHaveIs2Hiden("D:\\SampleVFP");
            FileHaveIs2Hiden("R:\\DRVD");
            FileHaveIs2Hiden("E:\\_Cloud");
        }


        static void FileHaveIs2Hiden(string cfFolder) //  ทำไฟล์ FileHave.dir ให้เป็น Hidden  ยกเลิก
        {
            if (!Directory.Exists(cfFolder)) { return; }
            Console.WriteLine("Folder : " + cfFolder);
            FileAttributes attributes;
            DirectoryInfo dFolderFle = new DirectoryInfo(cfFolder);
            FileInfo[] cFileHave = dFolderFle.GetFiles("abceHave.dir", SearchOption.AllDirectories);
            Console.ForegroundColor = ConsoleColor.Yellow;
            foreach (FileInfo cFileList in cFileHave)
            {
                attributes = File.GetAttributes(cFileList.FullName);
                if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) // เป็น Hidden File แล้วไม่ต้องทำอะไร 
                {
                    // Show the file.
                    // attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
                    // File.SetAttributes(cFileList.FullName, attributes);
                    // Console.WriteLine("{0} File Is hidden.", cFileList.FullName);
                }
                else
                {
                    // Hide the file.
                    File.SetAttributes(cFileList.FullName, File.GetAttributes(cFileList.FullName) | FileAttributes.Hidden);
                    Console.WriteLine(" --> SET {0} Is Now Hidden.", cFileList.FullName);
                }
            }
            Console.ForegroundColor = ConsoleColor.White;
        }


        static void PrepareUyodbackup()
        {
            string p = "PrepareUyodbackup";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            DownloadFromWeb("http://uyod.net/exe/UnRAR.exe", "UnRAR.exe");
            DownloadFromWeb("http://uyod.net/exe/Rar.exe", "Rar.exe");
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            TextWriter Print2Txt = new StreamWriter(cFle4Text);
            Print2Txt.WriteLine("Start Backup  : " + DateTime.Now.ToString("dd/MM/yy HH:mm:ss"));
            Print2Txt.WriteLine("Read File     : uyodbackup.ini");
            Print2Txt.WriteLine("Folder Backup : " + FldGdrive);
            Print2Txt.WriteLine("Folder Source : " + FldBDPos);
            cTemp = "Shutdown      : " + nShutdown.ToString().Trim() + " = ";
            switch (nShutdown)
            {
                case (1): cTemp = cTemp + "Shutdown"; break;
                case (2): cTemp = cTemp + "Restart"; break;
                default: cTemp = cTemp + "No"; break;
            }
            Print2Txt.WriteLine(cTemp);
            Print2Txt.WriteLine("Wait          : " + nTime.ToString().Trim());
            Print2Txt.Close();
            if (lDebug) { Special.DEBUG.D(p," - 03"); }
            if (File.Exists("RenameInFuture.txt")) // เปลี่ยนชื่อไฟล์ที่เคยUpdateแล้ว เนื่องจาก C# ทำงานเร็วมาก 
            {
                ReadFileDos("RenameInFuture.txt");
                File.Delete("RenameInFuture.txt");
            }
            if (lDebug) { Special.DEBUG.D(p, " - 04 *FINNISH"); }
        }


        static void RunDosMode()  // ทำงานแบบ DOS Mode หลังจากสำรองเสร็จ 
        {
            string p = "RunDosMode";
            if (lDebug) { Special.DEBUG.D(p,"  - 01"); }
            string FLECommand = FldGdrive + "RunDosMode.txt";
            if (!File.Exists(FLECommand))
            {
                if (lDebug) { Special.DEBUG.D(p," - 02"); }
                TextWriter Print2Txt = new StreamWriter(FLECommand);
                Print2Txt.WriteLine("");
                Print2Txt.WriteLine("");
                Print2Txt.WriteLine("");
                Print2Txt.WriteLine("");
                Print2Txt.WriteLine("");
                Print2Txt.Close();
                Print2Txt.Dispose();
            }
            ReadFileDos(FLECommand);
            if (Directory.Exists("D:"))
            {
                if (!Directory.Exists("D:\\_Datanet")) { Directory.CreateDirectory("D:\\_Datanet"); }
                if (lDebug) { Special.DEBUG.D(p, " - 03"); }
                TextWriter Print2Txt = new StreamWriter("D:\\_Datanet\\Transac.zp");
                Print2Txt.WriteLine(DateTime.Now.ToString());
                Print2Txt.Close();
                Print2Txt.Dispose();
            }
            if (Directory.Exists("E:"))
            {
                if (lDebug) { Special.DEBUG.D(p, " - 04"); }
                if (!Directory.Exists("E:\\_Datanet")) { Directory.CreateDirectory("E:\\_Datanet"); }
                TextWriter Print2Txt = new StreamWriter("E:\\_Datanet\\Transac.zp");
                Print2Txt.WriteLine(DateTime.Now.ToString());
                Print2Txt.Close();
                Print2Txt.Dispose();
            }
            if (lDebug) { Special.DEBUG.D(p, "  - 05 *FINNISH"); }
        }


        // FldGdrive + "backup2c.txt";
        static void BackupFinnish() 
        {
            string p = "BackupFinnish";
            if (lDebug) { Special.DEBUG.D(p,"  - 01"); }
            cTemp = FldGdrive + @"Backup";
            if (!Directory.Exists(cTemp)) { Directory.CreateDirectory(cTemp); }
            if (lDebug) { Special.DEBUG.D(p,"  - 02"); }
            DirectoryInfo dFolderProg = new DirectoryInfo(FldGdrive);
            FileInfo[] FileList = dFolderProg.GetFiles("*.txt", SearchOption.TopDirectoryOnly);
            foreach (FileInfo cFileList in FileList)
            {
                cTemp2 = cTemp + "\\" + cFileList.Name;
                if (CompareFile(cFileList.FullName, cTemp2))
                {
                    try
                    {
                        File.Copy(cFileList.FullName, cTemp2, true);
                        Console.WriteLine("Copy Complete " + cFileList.FullName + " -> " + cTemp2);
                    }
                    catch { Console.WriteLine("Copy Error " + cFileList.FullName + " -> " + cTemp2); }
                }
            }
            cTemp = ""; cTemp2 = "";
            if (lDebug) { Special.DEBUG.D(p,"  - 03"); }
        }

        static void ReadFileDos(string FLEInDos)
        {
            string p = "ReadFileDos";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            Console.WriteLine("");
            Console.WriteLine("Read File " + FLEInDos);
            string[] lines = System.IO.File.ReadAllLines(FLEInDos);
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            foreach (string line in lines)
            {
                if (line.Trim() != "")
                {
                    Console.WriteLine("\t" + line.Trim());
                    ExecuteCommand(line.Trim());
                }
            }
            if (lDebug) { Special.DEBUG.D(p, " - 03 *FINNISH"); }
            Console.WriteLine("Read Finish " + FLEInDos);
        }


        static void ExecuteCommand(string command)
        {
            string p = "ExecuteCommand";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            int ExitCode;
            ProcessStartInfo ProcessInfo;
            Process Process;
            ProcessInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
            ProcessInfo.CreateNoWindow = true;
            ProcessInfo.UseShellExecute = false;
            Process = Process.Start(ProcessInfo);
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            Process.WaitForExit();
            ExitCode = Process.ExitCode;
            Process.Close();
            if (lDebug) { Special.DEBUG.D(p, " - 03 *FINNISH"); }
        }


        static void Copy2FlashDrive()
        {
            if (mFlashDrive == "") { return; }
            if (!Directory.Exists(mFlashDrive)) { UyodMess("ไม่พบ Flash Drive " + mFlashDrive); return; }
            mFlashDrive = mFlashDrive + "\\Backup" + FldBDPos.Substring(2) + DateTime.Now.ToString("ddd") + "\\";
            if (!Directory.Exists(mFlashDrive)) { Directory.CreateDirectory(mFlashDrive); }

            string FleBackup, FleCopy;
            FleBackup = FldGdrive + "Backup";
            if (Directory.Exists(FleBackup)) // เริ่ม Copy ไป Flash Drive 
            {
                DirectoryInfo dFolderProg = new DirectoryInfo(FleBackup);
                FileInfo[] FileList = dFolderProg.GetFiles("*.rar", SearchOption.TopDirectoryOnly);
                foreach (FileInfo cFileList in FileList)
                {
                    FleCopy = cFileList.Name.Trim();
                    FleCopy = mFlashDrive + FleCopy.Substring(0, FleCopy.Length - 4) + ".rar";
                    if (CompareFile(cFileList.FullName, FleCopy))
                    {
                        try {
                            File.Copy(cFileList.FullName, FleCopy, true);
                            Console.WriteLine("Copy Complete ," + cFileList.FullName+" -> "+FleCopy);
                        }
                        catch { Console.WriteLine("Copy Error ," + cFileList.FullName + " -> " + FleCopy); }
                    }
                }
            }

            FleBackup = FldGdrive + "Backup\\System";
            mFlashDrive = mFlashDrive + "System\\";
            if (!Directory.Exists(mFlashDrive)) { Directory.CreateDirectory(mFlashDrive); }
            if (Directory.Exists(FleBackup)) // เริ่ม Copy ไป Flash Drive 
            {
                DirectoryInfo dFolderProg = new DirectoryInfo(FleBackup);
                FileInfo[] FileList = dFolderProg.GetFiles("*.rar", SearchOption.TopDirectoryOnly);
                foreach (FileInfo cFileList in FileList)
                {
                    FleCopy = cFileList.Name.Trim();
                    FleCopy = mFlashDrive + FleCopy.Substring(0, FleCopy.Length - 4) + ".uyod";
                    if (CompareFile(cFileList.FullName, FleCopy))
                    {
                        try
                        {
                            File.Copy(cFileList.FullName, FleCopy, true);
                            Console.WriteLine("Copy Complete ," + cFileList.FullName + " -> " + FleCopy);
                        }
                        catch { Console.WriteLine("Copy Error ," + cFileList.FullName + " -> " + FleCopy); }
                       
                    }
                }
            }
        }


        static void Compare4Gdrive()  // เปรียบเทียบ GDrive 
        {
            string GDrive = @"G:\My Drive\";  // บนคลาวด์ 
            string vGDrive = @"V:\_My Cloud\G Drive\"; // บน PC
            string cSubfix = "", cFolderCr = "";
            int nGDrive = GDrive.Length;
            int nvGDrive = vGDrive.Length;
            bool lSubfix;

            if (!Directory.Exists(GDrive)) { UyodMess("ไม่พบ Folder " + GDrive); return; }
            if (!Directory.Exists(vGDrive)) { UyodMess("ไม่พบ Folder " + vGDrive); return; }
            
            DirectoryInfo dFolderProg;
            FileInfo[] FileList;
            for (int i4Check = 1; i4Check <= 2; i4Check++)
            {
                // 1=จาก Cloud ไปที่ Harddisk   2= จาก Cloud ไปที่ Harddisk  
                //  if (i4Check == 1)  {   } else  {   }
                if (i4Check == 1) { dFolderProg = new DirectoryInfo(GDrive); } else { dFolderProg = new DirectoryInfo(vGDrive); }
                FileList = dFolderProg.GetFiles("*.*", SearchOption.AllDirectories);
                foreach (FileInfo cFileList in FileList)
                {
                    cTemp = cFileList.FullName;
                    if (i4Check == 1) { cTemp2 = vGDrive + cFileList.FullName.Substring(nGDrive); } else { cTemp2 = GDrive + cFileList.FullName.Substring(nvGDrive); }
                    cSubfix = cFileList.Name.Substring(cFileList.Name.Length - 4);
                    // ตรวจสอบนามสกุลไม่ต้อง Copy 
                    switch (cSubfix)
                    {
                        case (".ini"): lSubfix = false; break;
                        case (".lnk"): lSubfix = false; break;
                        case (".rar"): lSubfix = false; break;
                        case (".zip"): lSubfix = false; break;
                        case ("site"): lSubfix = false; break;
                        case ("heet"): lSubfix = false; break;
                        case ("gdoc"): lSubfix = false; break;
                        default: lSubfix = true; break;
                    }
                    // โพลเดอร์ Cache ก็ไม่ต้อง Copy 
                    if (cFileList.DirectoryName.Contains("content_cache")) { lSubfix = false; }
                    Console.WriteLine(cTemp);
                    if (lSubfix && CompareFile(cTemp, cTemp2))
                    {
                        Console.WriteLine("**********");
                        Console.WriteLine(cTemp + "  ," + File.GetLastWriteTime(cTemp));
                        Console.WriteLine(cTemp2 + ">" + File.GetLastWriteTime(cTemp2));
                        Console.WriteLine("Fodler " + cTemp2.Substring(0, cTemp2.Length - cFileList.Name.Length - 1));
                        Console.Write("  --> Diferrence");
                        try
                        {
                            cFolderCr = cTemp2.Substring(0, cTemp2.Length - cFileList.Name.Length - 1);
                            if (!Directory.Exists(cFolderCr)) { Directory.CreateDirectory(cFolderCr); }
                            File.Copy(cTemp, cTemp2, true);
                        }
                        catch (Exception ex) { Console.WriteLine("Copy File Error : " + ex.Message); }
                    }
                }
            }
        }

        private static string DiskBackup(int nDisk, int nOption)
        {
            string p = "DiskBackup";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            string retDisk = "", mHDtype = "FIXED", mFld2Back = @"_Cloud\";
            // 1=HD 2 =FD 3=CD  4=Netwaork Drive 
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            switch (nDisk)
            {
                case (1): mHDtype = "FIXED"; break; // Harddisk 
                case (2): mHDtype = "REMOVABLE"; mFld2Back = @"Backup\"; break; // FlashDrive Removable
                case (3): mHDtype = "CDROM"; break; // CD 
                case (4): mHDtype = "XXXX"; break; // NetworkDrive 
            }
            if (lDebug) { Special.DEBUG.D(p," - 03"); }
            DriveInfo[] allDrives = DriveInfo.GetDrives();
            foreach (DriveInfo hd in allDrives)
            {
                if (hd.IsReady && hd.DriveType.ToString().ToUpper() == mHDtype)
                {
                    if (nOption == 1)
                    { // เช่น E:\_Cloud\Customer\
                        if (nDisk == 1) { retDisk = hd.Name + mFld2Back + MyCompany + @"\"; } else { retDisk = hd.Name; }
                    }
                    if (nOption == 2 && hd.VolumeLabel == "REMOVEABLE") { retDisk = hd.Name; break; }
                    if (nOption == 2 && nDisk == 2) { retDisk = hd.Name; }
                    if (hd.VolumeLabel.ToUpper().Trim() == "BACKUP") { break; } // ถ้าเจอ Label ชื่อ Backup ให้ ออกมาเลย 
                }
            }
            if (lDebug) { Special.DEBUG.D(p," - 04"); }
            if (inharddisk && nOption == 1 && Directory.Exists("V:")) // Run กับ Source .เปลี่ยน Folder 
            {
                switch (nDisk)
                {
                    case (1): retDisk = @"V:\_My Cloud\My Drive\"; break;
                    case (2): retDisk = @"V:\_My Cloud\Flash Drive"; break;  // ไม่ต้องมี \ 
                }
            }
            if (lDebug) { Special.DEBUG.D(p," - 05"); }
            if (retDisk!="" &&  !Directory.Exists(retDisk.Substring(0, 2))) { retDisk = ""; }
            if (retDisk != "" && !Directory.Exists(retDisk)) { Directory.CreateDirectory(retDisk); } // ให้สร้าง HD Drive ถ้าหาไม่เจอ 
            if (lDebug) { Special.DEBUG.D(p, " - 06 *FINNISH"); }
            return retDisk;
        }

        // หลังจากทำงานเสร็จให้ ทำ Exe หรือ Bat ต่อ     
        static void After2Execute()
        {
            string p = "After2Execute";
            if (lDebug) { Special.DEBUG.D(p,"- 01"); }
            string FleExeAct = FldGdrive + "Backup\\Execute\\";
            DirectoryInfo dFolderList;
            FileInfo[] FileList;
            TextWriter List2Write;
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            if (!Directory.Exists(FleExeAct)) { Directory.CreateDirectory(FleExeAct); }
            if (lDebug) { Special.DEBUG.D(p," - 03"); }
            List2Write = new StreamWriter(FldGdrive + "Backup\\" + "After2Execute.txt");
            List2Write.WriteLine(MyApp);
            List2Write.WriteLine(FleExeAct);
            List2Write.WriteLine("@" + DateTime.Now.ToString("dd/MM/yy HH:mm"));
            List2Write.WriteLine("");
            if (lDebug) { Special.DEBUG.D(p," - 04"); }
            // ตรวจสอบ Runtime ของ Fpw26
            if (File.Exists(@"D:\BdPos\FOXW2600.ESL") && !File.Exists(FleExeAct + "FOXW2600.ESL"))
            { File.Copy(@"D:\BdPos\FOXW2600.ESL", FleExeAct + "FOXW2600.ESL"); }
            // ตรวจสอบ Runtime ของ Visual Fox 6       
            if (File.Exists(@MyFolderApp+"\\VFP6ENU.DLL") && !File.Exists(FleExeAct + "VFP6ENU.DLL"))
            { File.Copy(@MyFolderApp+"\\VFP6ENU.DLL", FleExeAct + "VFP6ENU.DLL"); }
            if (File.Exists(@MyFolderApp+"\\VFP6KOR.DLL") && !File.Exists(FleExeAct + "VFP6KOR.DLL"))
            { File.Copy(@MyFolderApp+"\\VFP6KOR.DLL", FleExeAct + "VFP6KOR.DLL"); }
            if (File.Exists(@MyFolderApp+"\\VFP6R.DLL") && !File.Exists(FleExeAct + "VFP6R.DLL"))
            { File.Copy(@MyFolderApp+"\\VFP6R.DLL", FleExeAct + "VFP6R.DLL"); }
            if (File.Exists(@MyFolderApp+"\\VFP6RENU.DLL") && !File.Exists(FleExeAct + "VFP6RENU.DLL"))
            { File.Copy(@MyFolderApp+"\\VFP6RENU.DLL", FleExeAct + "VFP6RENU.DLL"); }
            if (File.Exists(@MyFolderApp+"\\VFP6RKOR.DLL") && !File.Exists(FleExeAct + "VFP6RKOR.DLL"))
            { File.Copy(@MyFolderApp+"\\VFP6RKOR.DLL", FleExeAct + "VFP6RKOR.DLL"); }
            if (lDebug) { Special.DEBUG.D(p," - 05"); }
            dFolderList = new DirectoryInfo(FleExeAct);
            FileList = dFolderList.GetFiles("*.*", SearchOption.AllDirectories);
            if (lDebug) { Special.DEBUG.D(p," - 06"); }
            string[] Flebat;
            foreach (FileInfo cFileList in FileList)
            {
                switch (cFileList.Extension.ToLower())
                {
                    case (".bat"):
                    case (".exe"):
                        Console.WriteLine(cFileList.FullName + " ...Active ");
                        Util.Program.Pok_Process(cFileList.FullName, "");
                        List2Write.WriteLine(cFileList.FullName);
                        break;
                }
                if (lDebug) { Special.DEBUG.D(p," - 07"); }
                if (cFileList.Extension.ToLower() == ".bat")
                {
                    string[] FleTxt = File.ReadAllLines(cFileList.FullName);
                    foreach (string s in FleTxt) { List2Write.WriteLine("  " + s); }
                }
            }
            List2Write.Close(); List2Write.Dispose();
            if (lDebug) { Special.DEBUG.D(p," - 08"); }
        }

        static void Source2Uyoddotnet()  // Upload ไป uyod.net/source
        {
            // ho_p.rar
            string p = "Source2Uyoddotnet";
            if (lDebug) { Special.DEBUG.D(p," - 01"); }
            string sourcedrv = "R:\\BDBackup\\";
            string clouddrv = "R:\\Temp\\Source2uyod\\";
            string fle="",fle1,fle2;
            string clouddate = "_"+DateTime.Now.ToString("ddd").ToLower();
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            if (Directory.Exists("R:\\Temp") && Directory.Exists(sourcedrv)) // 
            if (lDebug) { Special.DEBUG.D(p," - 03"); }
            {   if (!Directory.Exists(clouddrv)) { Directory.CreateDirectory(clouddrv); }
                for (int i= 1;i<=14;i++)
                {
                    switch (i)
                    { case (1): fle="ho_p";break; case (2): fle = "poswin_p"; break; case (3): fle = "formail_p"; break;
                      case (4): fle = "sc_p"; break; case (5): fle = "data_p"; break;  case (6): fle = "transac_p"; break;
                      case (7): fle = "util_p"; break; case (8): fle = "ho_dbfrun_p"; break;  case (9): fle = "poswin_dbfrun_p"; break;
                      case (10): fle = "formail_dbfrun_p"; break; case (11): fle = "tranorg_p"; break;  case (12): fle = "masorg_p"; break;
                     case (13): fle = "MyInternet"; break; case (14): fle = "MyC.rar"; break; 
                    }
                    fle1 = sourcedrv + fle + ".rar"; fle2 = clouddrv + fle + clouddate + ".rar";
                    if (File.Exists(fle1) && Util.Program.CompareDTFile(0,fle1, fle2))
                    {
                        File.Copy(fle1, fle2, true);
                        Console.WriteLine("   Copy " + fle1); Console.WriteLine("   To " + fle2 + " Complete");
                        if (i<=6 ) { Util.Program.FTPtoUyoddotnet(0, fle2, "source", fle + clouddate + ".rar"); }
                        else { Util.Program.FTPtoUyoddotnet(0, fle1, "source", fle+ ".rar"); }
                        if (lDebug) { Special.DEBUG.D(p," - 04"); }
                    }
                }
            }
            if (lDebug) { Special.DEBUG.D(p, " - 04 *FINNISH"); }
        }


        static void Test_Prog()     // Pipop
        {
            // Source2Uyoddotnet();
            string p = "Test_Prog";
            if (lDebug) { Special.DEBUG.D(p, " - 01"); } 
            Console.WriteLine("");
            Console.WriteLine("Test Programn");
            Console.Write("  ....Press Any Key");
            // Console.ReadLine();
            if (lDebug) { Special.DEBUG.D(p, " - 02 *FINNISH"); }
        }

        static void Main(string[] args)
        {
            Init();
            string p = "Main";
            if (lDebug) { Special.DEBUG.D(p," - 01"); } 
            if (MyApp.Substring(0, 2) == "V:") { insource = true; } // Run กับ Source
            if (Directory.Exists(@"R:\Temp")) { inharddisk = true; }  // Run กับ Harddisk   
            Console.Title = "สำรองข้อมูล";
            string cpFlag;
            // Pipop ปรกติ B ทำงานจริงให้เปลี่ยนเป็น B ,T,S สำหรับทดสอบ S Cop y Source Code
            if (lDebug) { Special.DEBUG.D(p," - 02"); }
            if (args.Length == 0) { if (insource) { cpFlag = "T"; } else { cpFlag = "B"; } }  // Pipop cpFlag = "T"
            else { cpFlag = args[0].Trim(); }
            cpFlag = cpFlag.ToUpper();
            nForWork = 1;  //  ปรกติ  1 nForWork=0 ไม่ทำอะไรเลย ,1 ทำงานตามปรกติ 2 ไม่Runโปรแกรมและมีเตือน  
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"); // เปลี่ยนรูปแบบวันที่ เป็น GB
            if (lDebug) { Special.DEBUG.D(p," - 03"); }
            if (File.Exists("uyodbackup.ini"))
            {
                if (lDebug) { Special.DEBUG.D(p," - 04"); }
                string[] FleTxt = File.ReadAllLines("uyodbackup.ini");
                FldGdrive = FleTxt[0].Trim();  // Drive Backup 
                FldBDPos = FleTxt[1].Trim(); // Drive ต้นทาง
                nShutdown = int.Parse(FleTxt[2].Trim()); // 1 2 เพื่อ รีสตาร์ท หรือ Shutdown 
                nTime = int.Parse(FleTxt[3].Trim()); // เวลาในการ รีสตาร์ท หรือ Shutdown  (วินาที)
                mFlashDrive = FleTxt[4].Substring(0, 2).Trim();   // Flash drive 
                if (FleTxt[5].Trim() != "") { MyCompany = FleTxt[5].Trim(); }  // ชื่อบริษัท 
                if (lDebug) { Special.DEBUG.D(p," - 05"); }
                // ตรวจสอบ โพลเดอร์ ต้องมี // มาด้วย 
                if (FldGdrive.Substring(FldGdrive.Length - 1) != @"\") { FldGdrive = FldGdrive + @"\"; }
                if (FldBDPos.Substring(FldBDPos.Length - 1) != @"\") { FldBDPos = FldBDPos + @"\"; }
                if (!(nTime >= 1 && nTime <= 999)) { nTime = 60; }
                if (mFlashDrive == "" || mFlashDrive.Substring(1, 1) != ":")
                { // กรณีไม่พบ Flash Drive ให้โปรแกรมหา 
                    cTemp = DiskBackup(2, 1); if (cTemp != "") { mFlashDrive = cTemp; }
                    cTemp = "";
                }
                if (!Directory.Exists(mFlashDrive)) { mFlashDrive = ""; }
            }
            else
            {
                WriteUyodbackup();
                return;
            }
            if (lDebug) { Special.DEBUG.D(p," - 06"); }
            if (cpFlag == "B")  // กรณีไม่เจอDriveปลายทาง ให้หาDrive สำรอง
            {
                if (!Directory.Exists(FldGdrive) || FldGdrive == "")
                {
                    // Directory.Exists(FldGdrive.Substring(0, 2))
                    if (Util.Program.Pok_CheckDRV(FldGdrive)) { Directory.CreateDirectory(FldGdrive); } // ถ้าเจอ Drive ให้สร้างโพลเดอร์
                    else
                    {
                        cTemp = DiskBackup(1, 1); // กรณีไม่เจอ Drive ให้โปรแกรมหาอัตโนมัติ 
                        if (cTemp != "")
                        { UyodMess("ไม่พบโพลเดอร์ "+FldGdrive+Environment.NewLine+Environment.NewLine
                            + "เปลี่ยนมาสำรองที่ "+cTemp+" แทน");
                            FldGdrive = cTemp; } 
                        else { return; }
                        cTemp = "";
                    }
                }
            }
            if (lDebug) { Special.DEBUG.D(p," - 07"); }
            // ตรวจสอบครั้งสุดท้าย กัน Error เรื่อง Drive 
            if (FldGdrive == "" || !Util.Program.Pok_CheckDRV(FldGdrive))
            { Util.Program.Pok_Mess("ไม่พบโพลเดอร์ "+FldGdrive+" แก้ไขไฟล์ uyodbackup.ini"); return; }
            if (!Directory.Exists(FldGdrive + "Backup")) { Directory.CreateDirectory(FldGdrive + "Backup"); }
            cFle4Error = FldGdrive + "Backup\\" + MyProg + cFle4Error;
            Util.Program.Pok_Error(1, cFle4Error, "");
            cFle4Text = FldGdrive + "backup2c.txt";
            if (lDebug) { Special.DEBUG.D(p," - 08"); }
            switch (cpFlag) // Switch
            {
                case ("?"):
                case ("0"):
                case ("H"):
                    if (lDebug) { Special.DEBUG.D(p, " - 08-H"); }
                    Console.Clear();
                    Console.WriteLine("uyodbackup");
                    Console.WriteLine("Variable");
                    Console.WriteLine("-FldBDPos  = " + FldBDPos);
                    Console.WriteLine("-FldGdrive = " + FldGdrive);
                    Console.WriteLine("-nShutdown    = " + nShutdown.ToString());
                    Console.WriteLine("-nTime        = " + nTime.ToString());
                    Console.WriteLine("");
                    Console.WriteLine("uyodbackup     -> Backup For Customer");
                    Console.WriteLine("uyodbackup B_D -> Delete JunkFile In Bdpos And Source");
                    Console.WriteLine("uyodbackup C   -> Copy In copyfleintxt.txt");
                    Console.WriteLine("uyodbackup D   -> Delete In delfleintxt.txt");
                    Console.WriteLine("uyodbackup F   -> Backup For Source Code To Flashdrive");
                    Console.WriteLine("uyodbackup S   -> Backup For Source Code");
                    Console.WriteLine("uyodbackup W   -> Write uyodbackup.ini Again");
                    Console.WriteLine("");
                    Console.WriteLine("Remark : Rar u C:\\Rar\\test *.dbf *.cdx ...");
                    Console.WriteLine("Remark : nShutdown=1 Shutdown =2 Restart");
                    Console.WriteLine("");
                    Console.WriteLine("Type Exit Or Click X For Close");
                    Console.ReadLine();
                    Readme();
                    break;
                case ("R"):
                    break;
                case ("T"):
                    Test_Prog();  
                    break;
                case ("C"): // Copyไฟล์ใน copyfleintxt.txt
                    CopyFleIntxt();
                    break;
                case ("D"): // ลบไฟล์ใน delfleintxt.txt
                    DelFleIntxt();
                    break;
                case ("W"):
                    WriteUyodbackup();  // เขียนไฟล์ uyodbackup.ini
                    return;
                case ("F"): // สำรอง SourceCode ไป FlashDrive 
                    SourceCode2FlashDrive();
                    break;
                case ("O"): // สำรอง SourceCode ไป R:\BdBackup แบบออฟไลน์  
                case ("S"): // สำรอง SourceCode ไป R:\BdBackup
                    if (lDebug) { Special.DEBUG.D(p, " - 08-S"); }
                    mRarText = @"R:\BdBackup\rarCommand.txt";
                    TextWriter ReadmeTxt = new StreamWriter(mRarText);
                    ReadmeTxt.WriteLine("Backup @" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
                    ReadmeTxt.Close();
                    ReadmeTxt.Dispose();
                    if (cpFlag == "O") {
                        SourceCodeBackup(0);
                        nTime = 60 * 1; // 1 นาที
                        cTemp = " -c " + (char)34 + "ระบบกำลังสำรองข้อมูล กรุณารอสักครู่"
                            + Environment.NewLine + "พัฒนาโดย Uyod Team" + (char)34;
                        Console.WriteLine("Wait... For Shutdown");
                        Source2Uyoddotnet();
                        Process2Arg("shutdown", "-s -t " + nTime.ToString().Trim() + cTemp, "");
                        if (pressYesNO("Don't Shutdown")) { Process2Arg("shutdown", "-a", ""); }
                    }
                    else { SourceCodeBackup(1); }   
                    // Compare4Gdrive();   ยกเลิก ทำ Mirror แทน 
                    break;
                case ("B_D"): // ลบไฟล์ไม่ได้ใช้งานของเครื่องที่บ้าน 
                    if (lDebug) { Special.DEBUG.D(p, " - 08-B_D"); }
                    BdPos2JuckFile();
                    break;
                default:  //  สำรองข้อมูล ลูกค้า 
                    if (lDebug) { Special.DEBUG.D(p, " - 08-DAFAULT"); }
                    if (nForWork == 1) { StartTime(); }
                    Console.Title = "UYODTEAM BACKUP";
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("");
                    Console.WriteLine("Read File     : uyodbackup.ini");
                    Console.WriteLine("Folder Backup : " + FldGdrive + " (FldGdrive)");
                    Console.WriteLine("Folder Source : " + FldBDPos + " (FldBDPos)");
                    if (nShutdown == 0) { Console.WriteLine("Shutdown      : NO"); }
                    else { Console.WriteLine("Shutdown      : YES"); }
                    Console.WriteLine("Wait Shutdown : " + nTime.ToString().Trim());
                    Console.ForegroundColor = ConsoleColor.White;
                    if (!Directory.Exists(FldBDPos)) { UyodMess("ไม่พบโพลเดอร์โปรแกรม " + FldBDPos); return; }
                    mRarText = FldGdrive + "rarCommand.txt";
                    if (lDebug) { Special.DEBUG.D(p," - 09"); }
                    TextWriter Readme2Txt = new StreamWriter(mRarText);
                    Readme2Txt.WriteLine("Backup @" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
                    Readme2Txt.Close();
                    Readme2Txt.Dispose();
                    PrepareUyodbackup();
                    if (lDebug) { Special.DEBUG.D(p," - 10"); }
                    Backup2Gdrive(); // สำรองโปรแกรม BdPos , BdPosVFP สำรองข้อมูล  
                    if (lDebug) { Special.DEBUG.D(p," - 11"); }
                    // ยกเลิก  ใช้โปรแกรม _updatesoftware.exe แทน 
                    // UnRar2BdPos();  //ตรวจสอบไฟล์ปรับปรุงต่างๆ จะมีนามสกุล  Update\\???.rar  เพื่อ Unrar   
                    if (lDebug) { Special.DEBUG.D(p," - 12"); }
                    Copy2FlashDrive(); // Copy ลง Flash Drive อีกครั้ง 
                    if (lDebug) { Special.DEBUG.D(p," - 13"); }
                    MyOtherFolderBackup(); // สำรองจากโพลเดอร์อื่นๆ MyDBFOthertBackup.txt
                    if (lDebug) { Special.DEBUG.D(p," - 14"); }
                    MyDoc4Customer(); // สำรองเอกสารต่างๆของลูกค้า 
                    if (lDebug) { Special.DEBUG.D(p," - 15"); }
                    RunDosMode();  // ทำงานแบบ DOS Mode หลังจากสำรองเสร็จ  
                    if (lDebug) { Special.DEBUG.D(p," - 16"); }
                    After2Execute(); // หลังจากทำงานเสร็จให้ ทำ Exe หรือ Bat ต่อ     
                    if (lDebug) { Special.DEBUG.D(p," - 17"); }
                    BackupFinnish(); //  การทำงานสุดท้าย  
                    if (lDebug) { Special.DEBUG.D(p," - 18"); }
                    if (nForWork == 1) { FinishTime(); }
                    Console.WriteLine("");
                    Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Backup Complete");
                    cTemp2 = "วินโดร์จะ "+(char)39;
                    if (nShutdown == 2) { cTemp2 = cTemp2 + "รีสตาร์ท"; } else { cTemp2 = cTemp2 + "ปิดเครื่อง"; }
                    cTemp2 = cTemp2 +(char)39 +" ภายในเวลา "+ nTime.ToString().Trim()+" วินาที";
                    cTemp = " -c " +(char)34+"ระบบกำลังสำรองข้อมูล กรุณารอสักครู่"
                        + Environment.NewLine +cTemp2
                        + Environment.NewLine+"พัฒนาโดย Uyod Team"+ (char)34;
                    if (lDebug) { Special.DEBUG.D(p," - 19"); }
                    switch (nShutdown)
                    {
                        case (1): // Shutdown
                            Console.WriteLine("Wait... For Shutdown");
                            Process2Arg("shutdown", "-s -t " + nTime.ToString().Trim()+cTemp, "");
                            if (pressYesNO("Don't Shutdown")) { Process2Arg("shutdown", "-a", ""); }
                            break;
                        case (2): // Restart
                            Console.WriteLine("Wait... For Restart");
                            Process2Arg("shutdown", "-r -t " + nTime.ToString().Trim()+cTemp, "");
                            if (pressYesNO("Don't Restart")) { Process2Arg("shutdown", "-a", ""); }
                            break;
                    }
                    if (lDebug) { Special.DEBUG.D(p," - 20"); }
                    Console.ForegroundColor = ConsoleColor.White;
                    if (lDebug) { Special.DEBUG.D(p," - 21 **FINNISH PROGRAMN"); }
                    break;  // จบ Defalut 
            }
        }
    }
}
