using System;
using System.IO;
using System.Net;
using Shell32;
using System.Windows.Forms;
using IWshRuntimeLibrary;
using System.Text;
using System.Diagnostics;


namespace Util_pok
{
    static class Program
    {
        public class MyGlobal
        {
            public static int insource = 0;
            public static string cpFlag;
            public static string cpWord1;
            public static string cpWord2;
            public static string cpText1;  // ชุดคำที่ 1 เป็นต้นไป
            public static string cpText2;  // ชุดคำที่ 2 เป็นต้นไป
            public static string cpMessage; // เหมือน  
        }


        private static void UtilPOKReadMe() // Pipop
        {
            MessageBox.Show(
            "util_pok 1 ชื่อไฟล์ ชื่อShortcut  --->สร้างShortcut  " + System.Environment.NewLine
            + "util_pok 2 0 0  --->สร้าง Short-cut จาก backup2c,upnet2c,todos,restart,shutdown ในคลาว์" + System.Environment.NewLine
            + "util_pok 3 0 0  --->ล้างไฟล์ nasoh เฉพาะโพลเดอร์" + System.Environment.NewLine
            + "util_pok 3 1 0  --->ทั้งหมด" + System.Environment.NewLine
            + "util_pok 4 0 0  --->ล้างไฟล์ขยะของ D:\\Bdpos\\ และ D:\\BdposVFP\\" + System.Environment.NewLine
            + "util_pok 5 T M  --->Line Notify  M=* ขึ้นบรรทัดใหม่" + System.Environment.NewLine
            + "util_pok 6 T M+0+0+Picture " + System.Environment.NewLine
            + "             --->Line Notify Token ข้อความ+stickerPackageID+stickerID+Picture"+System.Environment.NewLine
            + "             เช่น util_pok 6 Token Pipop*Youyod+0+0+http://uyod.net/logo.jpg" + System.Environment.NewLine
            + "             เช่น util_pok 6 Token Pipop*Youyod+0+0+" + System.Environment.NewLine
            + "util_pok 7 0 0  --->สภาพแวดล้อมของวินโดร์" + System.Environment.NewLine
            );
        }

        static void Pok_Mess(string cfWord)
        {
            if (MyGlobal.insource==0) { return; }
            MessageBox.Show(cfWord + System.Environment.NewLine
            + "" + System.Environment.NewLine
            + "" + System.Environment.NewLine
            , "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }


        private static void SetupVar()
        {
            if (System.IO.Directory.Exists(@"J:\Entertain\Movie\Private")) { MyGlobal.insource = 1; }
        }


        private static void crShortcutPOK(int nFlag,string TargetFile,string SubfixFile, string ShortcutName,string IconFileRun)
        {
            ShortcutName = ShortcutName.Trim();
            IconFileRun = IconFileRun.Trim();
            TargetFile = TargetFile.Trim();
            string ShortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // Desktop
            string IconFile= TargetFile.Substring(0, TargetFile.Length - 4) + ".ico";

            switch (nFlag)
            {
                case (1):
                    break;
                case (2):
                    TargetFile = System.IO.Directory.GetCurrentDirectory()+"\\"+TargetFile;
                    IconFile = System.IO.Directory.GetCurrentDirectory() + @"\UyodTeam\Ico\"+IconFile;
                    break;
            }

            // nFlag = 2 ถ้าไม่เจอไฟล์ไอคอนให้ออกไปก่อน 
            if (nFlag == 2 && !System.IO.File.Exists(IconFile)) { return; }
            
            // ส่งไฟล์Iconมาด้วยให้เอาไฟล์นั้นใช้แทน
            if (IconFileRun != "" && System.IO.File.Exists(IconFileRun)) { IconFile = IconFileRun; }

            if (MyGlobal.insource==1)
            {
                Pok_Mess("ShortcutName = " + ShortcutName 
                + System.Environment.NewLine + " , TargetFile = " + TargetFile
                +System.Environment.NewLine + " , IconFile = " + IconFile
                + System.Environment.NewLine + " , ShortcutPath = " + ShortcutPath
                );
            }
            // ลบไอคอนถ้ามีไฟล์หายไป
            if (!System.IO.File.Exists(TargetFile) && System.IO.File.Exists(ShortcutPath + "\\" + ShortcutName + ".lnk"))
            { System.IO.File.Delete(ShortcutPath + "\\" + ShortcutName + ".lnk"); return; }

            if (!System.IO.File.Exists(TargetFile)) { Pok_Mess("ไม่พบไฟล์ "+TargetFile); return; }
            if (System.IO.File.Exists(ShortcutPath+"\\"+ShortcutName + ".lnk")) { return; }

            string ShortcutLocation = System.IO.Path.Combine(ShortcutPath, ShortcutName + ".lnk");
            WshShell shell = new WshShell();
            IWshShortcut crShortcut = (IWshShortcut)shell.CreateShortcut(ShortcutLocation);
            // โพลเดอร์ไฟล์ที่ทำงาน 
            crShortcut.WorkingDirectory = System.IO.Directory.GetCurrentDirectory();
            // อธิบายรายละเอียด
            crShortcut.Description = "สร้างโดย C# UyodTeam";   // Comment ใน Shortcut
            if (System.IO.File.Exists(IconFile))
            {
                crShortcut.IconLocation = IconFile ;   // icon ที่แสดงใน Desktop
            }
            if (SubfixFile != "") { TargetFile = TargetFile + " " + SubfixFile.Trim(); }
            crShortcut.TargetPath = TargetFile;         // 
            crShortcut.Save();
        }


        private static void crWindowsIcon()
        {
            string ShortcutName = "เข้าสู่ DOS";
            string ShortcutLocation = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), ShortcutName + ".lnk");
            WshShell shell = new WshShell();
            IWshShortcut crShortcut = (IWshShortcut)shell.CreateShortcut(ShortcutLocation);
            crShortcut.Description = "สร้างโดย C# UyodTeam";   // Comment ใน Shortcut
            crShortcut.IconLocation = System.IO.Directory.GetCurrentDirectory()+ "\\UyodTeam\\Ico\\todos.ico";   // icon ที่แสดงใน Desktop
            crShortcut.TargetPath = @"C:\Windows\System32\cmd.exe";
            crShortcut.WorkingDirectory = @"D:\Bdpos";
            crShortcut.Save();
        }


        private static void DownLoadIco()
        {
            string FileOnUyod = "http://www.uyod.net/ico/";
            string SaveToFolder = @"UyodTeam\Ico\";
            string SaveToFile = "";
            WebClient myWebClient = new WebClient();

            SaveToFile = "backup2c.ico";  
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "backup2c2.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "card.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "desktop-1.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "desktop-2.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "fix.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "filetran.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "food-1.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "food-2.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "food-3.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "food-4.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "food-5.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "hd.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "ho2.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "home.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "home2.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "hotel-1.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "hotel-2.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "key-1.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "key-2.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "key-3.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "man-1.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "man-2.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "poswin.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "traffic-1.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "upnet2c.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "todos.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "todos.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "print.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "restart.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "shutdown.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            myWebClient.Dispose();
        }


        private static void crShortcutAuto(int nCr) // เสริมระบบ backup2c.bat
        {
            if (!System.IO.Directory.Exists(@"UyodTeam\Ico")) { System.IO.Directory.CreateDirectory(@"UyodTeam\Ico");}
            if (!System.IO.File.Exists(@"UyodTeam\Ico\shutdown.ico")) { DownLoadIco(); }
            if (System.IO.File.Exists("backup2c.bat")) { crShortcutPOK(2, "backup2c.bat","", "สำรองข้อมูลทุกวัน",""); }
            if (System.IO.File.Exists("upnet2c.bat")) { crShortcutPOK(2, "upnet2c.bat", "", "ปรับปรุงผ่านคลาวด์",""); }
            if (System.IO.File.Exists("todos.bat")) { crShortcutPOK(2, "todos.bat", "", "เข้า DOS",""); }
            if (System.IO.File.Exists("restart.bat")) { crShortcutPOK(2, "restart.bat", "", "รีสตาร์ทเครื่อง", ""); }
            if (System.IO.File.Exists("shutdown.bat")) { crShortcutPOK(2, "shutdown.bat", "", "ปิดเครื่อง", ""); }
            switch (nCr)
            {
                case (1):  break;
            }
        }

        private static void crTxtAuto()
        {
            string bdPath=@"D:\BdPos";
            if (!System.IO.Directory.Exists(bdPath)) { bdPath = @"C:\BdPos"; }
            if (!System.IO.Directory.Exists(bdPath)) { return; }
            bdPath = bdPath+@"\repoPOK.txt";
            if (System.IO.Directory.GetCurrentDirectory().Substring(0, 2) != "C:")
            {
                TextWriter Print2Txt = new StreamWriter(bdPath);
                Print2Txt.WriteLine(System.IO.Directory.GetCurrentDirectory()+"\\");
                Print2Txt.Close();
            }
        }

        private static void RenameNASOH(int nRename,string cfFoder)
        {
            cfFoder = cfFoder.Trim();
            if (cfFoder == "") { cfFoder = Directory.GetCurrentDirectory(); }
            if (!Directory.Exists(cfFoder)) { return; }
            DirectoryInfo dFolder=new DirectoryInfo(cfFoder);
            FileInfo[] fFolder;
            if (nRename == 0) { fFolder = dFolder.GetFiles("*.nasoh", SearchOption.TopDirectoryOnly); }
            else { fFolder = dFolder.GetFiles("*.nasoh", SearchOption.AllDirectories); }
            string cfSource,cflRnaos;
            foreach (FileInfo lFolder in fFolder)
            {
                cfSource = lFolder.FullName;
                cflRnaos = lFolder.FullName.Substring(0, lFolder.FullName.Length - 6);
                Console.WriteLine(cfSource);
                if (System.IO.File.Exists(cflRnaos)) { System.IO.File.Delete(cfSource); }
                else { System.IO.File.Move(cfSource, cflRnaos); }
            }           
        }


        private static void DrvRunPOK(int nFunction,string cDRV)
        {
            // Sample cDRV=@"C:\"
            if (!System.IO.Directory.Exists(cDRV)) { Pok_Mess("ไม่มี Drive "+cDRV); return; }
            RenameNASOH(0, cDRV);
            string cFolderCut;
            foreach (string sInFolder in Directory.GetDirectories(cDRV))
            {
                cFolderCut = sInFolder.Substring(3, 1);
                switch (cFolderCut)
                {
                    case ("$"):
                    case ("C"):
                    case ("D"):
                    case ("P"):
                    case ("R"):
                    case ("U"):
                    case ("W"):
                    case ("S"):
                        break;
                    default:
                        switch (nFunction)
                        {
                            case (1): RenameNASOH(1, sInFolder);break;
                        }
                       
                        break;
                }
            }
        }


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


        //  ลบไฟล์ต่างๆของ D:\BDPOS\ 
        private static void DelBDTmp()
        {
            string drvCloud = Environment.GetEnvironmentVariable("CLOUD_DRIVE");
            if (drvCloud != null && drvCloud != "")
            {
                DelBDTmpFle(drvCloud + "Backup", "*.zip", 30);
                DelBDTmpFle(drvCloud + "Backup", "*.rar", 30);
                DelBDTmpFle(drvCloud + "pdf", "*.pdf", 30);
                DelBDTmpFle(drvCloud + "Screen", "*.jpg", 30);
                DelBDTmpFle(drvCloud + "UyodTeam", "*.zip", 30);
                DelBDTmpFle(drvCloud + "UyodTeam", "*.rar", 30);
                DelBDTmpFle(drvCloud + "txt", "*.txt", 30);
                DelBDTmpFle(drvCloud + "txt", "*.bak", 30);
                DelBDTmpFle(@"C:\Temp", "*.*", 3);
            }
            // สำหรับ D:\BdPos
            DelBDTmpFle(@"D:\BdPos\Bakdata", "F*.DBF", 7);
            DelBDTmpFle(@"D:\BdPos\Bakdata", "F*.CDX", 7);
            DelBDTmpFle(@"D:\BdPos\Bakdata", "C*.DBF", 7);
            DelBDTmpFle(@"D:\BdPos\Bakdata", "C*.CDX", 7);
            DelBDTmpFle(@"D:\BdPos\Bakdata", "F*.TXT", 7);
            DelBDTmpFle(@"D:\BdPos\Temp", "*.DBF", 0);
            DelBDTmpFle(@"D:\BdPos\Temp", "*.TMP", 0);
            DelBDTmpFle(@"D:\BdPos\Temp", "*.CDX", 0);
            DelBDTmpFle(@"D:\BdPos\Error", "*.DBF", 7);
            DelBDTmpFle(@"D:\BdPos\Error", "*.CDX", 7);
            DelBDTmpFle(@"D:\BdPos\Error", "*.ERR", 7);
            DelBDTmpFle(@"D:\BdPos\_Error", "*.DBF", 7);
            DelBDTmpFle(@"D:\BdPos\_Error", "*.CDX", 7);
            DelBDTmpFle(@"D:\BdPos\_Error", "*.ERR", 7);
            DelBDTmpFle(@"D:\BdPos\DayInOut", "*.DBF", 7);
            DelBDTmpFle(@"D:\BdPos\DayInOut", "*.CDX", 7);
            DelBDTmpFle(@"D:\Temp", "*.*",30);
            DelBDTmpFle(@"D:\BdPos\HO2MACH", "*.txt", 3);
            // สำหรับ D:\BdPosVFP
            DelBDTmpFle(@"D:\BdPosVFP\Bakdata", "F*.DBF", 7);
            DelBDTmpFle(@"D:\BdPosVFP\Bakdata", "F*.CDX", 7);
            DelBDTmpFle(@"D:\BdPosVFP\Bakdata", "C*.DBF", 7);
            DelBDTmpFle(@"D:\BdPosVFP\Bakdata", "C*.CDX", 7);
            DelBDTmpFle(@"D:\BdPosVFP\Bakdata", "F*.TXT", 7);
            DelBDTmpFle(@"D:\BdPosVFP\Temp", "*.DBF", 0);
            DelBDTmpFle(@"D:\BdPosVFP\Temp", "*.TMP", 0);
            DelBDTmpFle(@"D:\BdPosVFP\Temp", "*.CDX", 0);
            DelBDTmpFle(@"D:\BdPosVFP\Error", "*.DBF", 7);
            DelBDTmpFle(@"D:\BdPosVFP\Error", "*.CDX", 7);
            DelBDTmpFle(@"D:\BdPosVFP\Error", "*.ERR", 7);
            DelBDTmpFle(@"D:\BdPosVFP\_Error", "*.DBF", 7);
            DelBDTmpFle(@"D:\BdPosVFP\_Error", "*.CDX", 7);
            DelBDTmpFle(@"D:\BdPosVFP\_Error", "*.ERR", 7);
            DelBDTmpFle(@"D:\BdPosVFP\DayInOut", "*.DBF", 7);
            DelBDTmpFle(@"D:\BdPosVFP\DayInOut", "*.CDX", 7);
            DelBDTmpFle(@"D:\BdPosVFP\HO2MACH", "*.txt", 3);
            DelBDTmpFle(@"E:\PDF Backup", "PDF*.pdf", 7);
            DelBDTmpFle(@"E:\Temp", "*.*", 3);
            DelBDTmpFle(@"M:\HO2MACH", "*.txt", 3);
        }


        static void OpenWebSite(String URLname)
        {
            var opentwebthis = new ProcessStartInfo("chrome.exe");  // iexplore.exe
            opentwebthis.Arguments = URLname;
            Process.Start(opentwebthis);
        }


        private static void LineNotify(string cToKen,string LineMessage)  
        {
            LineMessage = Text2Replace(LineMessage);
            if (MyGlobal.insource == 1)
            {
                Pok_Mess("ข้อความเตือนทางไลน์ " + System.Environment.NewLine + System.Environment.NewLine
                   + "ToKen : " + cToKen + System.Environment.NewLine + ",Message : " + LineMessage);
            }
            if (cToKen=="")
            { Pok_Mess("LineNotify Error: ค่า Token เป็นช่องว่าง");
                OpenWebSite("https://notify-bot.line.me/my/");
                return;
            } 
            if (LineMessage == "") {Pok_Mess("LineNotify Error: ข้อความ เป็นช่องว่าง");  return; }

            var request = (HttpWebRequest)WebRequest.Create("https://notify-api.line.me/api/notify");
            var postData = string.Format("message={0}",LineMessage);
            var data = Encoding.UTF8.GetBytes(postData);

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            request.Headers.Add("Authorization", "Bearer " + cToKen);

            using (var stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }
            var response = (HttpWebResponse)request.GetResponse();
            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
        }

        // เปลี่ยน * เป็น ขึ้นบรรทัดใหม่  Pipop
        private static string Text2Replace(string mText) 
        {
            string cWordTake, cWordFor;
            mText = mText.Trim();
            cWordFor = "";
            for (int i = 0; i < mText.Length; i++)
            {
                cWordTake = mText.Substring(i, 1);
                switch (cWordTake)
                {
                    case ("*"):
                        cWordFor = cWordFor + System.Environment.NewLine; break;
                    default:
                        cWordFor = cWordFor + cWordTake; break;
                }
            }
            return cWordFor;
        }

        private static void WinVironment()
        {
            // Check Public IP
            string url = "http://checkip.dyndns.org";
            System.Net.WebRequest req = System.Net.WebRequest.Create(url);
            System.Net.WebResponse resp = req.GetResponse();
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            string response = sr.ReadToEnd().Trim();
            string[] a = response.Split(':');
            string a2 = a[1].Substring(1);
            string[] a3 = a2.Split('<');
            string ipGlobal = a3[0];

            TextWriter WriteTxt = new StreamWriter("Environment.txt");
            WriteTxt.WriteLine("Username     : {0}", Environment.UserName);
            WriteTxt.WriteLine("Computername : {0}", Environment.MachineName);
            WriteTxt.WriteLine("Domain       : {0}", Environment.UserDomainName);
            WriteTxt.WriteLine("Directory    : {0}", Environment.CurrentDirectory);
            WriteTxt.WriteLine("OS(64BIT)    : {0}", Environment.Is64BitOperatingSystem);
            WriteTxt.WriteLine("Machine      : {0}", Environment.MachineName);
            WriteTxt.WriteLine("OS           : {0}", Environment.OSVersion);
            WriteTxt.WriteLine("Public IP    : {0}", ipGlobal);
            WriteTxt.Close();
        }

        // Pipop
        private static void LineNotify2(string cToKen,string cLineWordSet)
        {
            string LineMessage = "";
            int stickerPackageID = 0;
            int stickerID = 0;
            string pictureUrl = "";
            // แยกคำจาก LineMessage
            string cWordTake, cWordFor;
            cLineWordSet = cLineWordSet.Trim();
            cWordFor = "";
            int j = 0;
            for (int i = 0; i < cLineWordSet.Length; i++)
            {
                cWordTake = cLineWordSet.Substring(i, 1);
                switch (cWordTake)
                {
                    case ("+"):
                        j++;
                        switch (j)
                        {
                            case (1): LineMessage = cWordFor; break;
                            case (2): stickerPackageID = int.Parse(cWordFor)  ; break;  
                            case (3): stickerID = int.Parse(cWordFor); break;
                        }
                        cWordFor = "";
                        break;
                    case ("*"):
                        cWordFor = cWordFor + System.Environment.NewLine; break;
                    default:
                        cWordFor = cWordFor + cWordTake; break;
                }
            }
            //
            pictureUrl = cWordFor;// อันสุดท้าย

            LineMessage = Text2Replace(LineMessage);
            if (MyGlobal.insource == 1)
            {
                Pok_Mess("ข้อความเตือนทางไลน์ " + System.Environment.NewLine + System.Environment.NewLine
                +"cToKen :" + cToKen + System.Environment.NewLine
                + ",LineMessage :" + LineMessage + System.Environment.NewLine
                + ",stickerPackageID :" + stickerPackageID.ToString() + System.Environment.NewLine
                + ",stickerID :" + stickerID.ToString() + System.Environment.NewLine
                + ",pictureUrl :" + pictureUrl);
            }
            if (cToKen == "")
            {
                Pok_Mess("LineNotify Error: ค่า Token เป็นช่องว่าง");
                OpenWebSite("https://notify-bot.line.me/my/");
                return;
            }
            try
            {
                var request = (HttpWebRequest)WebRequest.Create("https://notify-api.line.me/api/notify");
                var postData = string.Format("message={0}", LineMessage);
                if (stickerPackageID > 0 && stickerID > 0)
                {
                    var stickerPackageId = string.Format("stickerPackageId={0}", stickerPackageID);
                    var stickerId = string.Format("stickerId={0}", stickerID);
                    postData += "&" + stickerPackageId.ToString() + "&" + stickerId.ToString();
                }
                if (pictureUrl != "")
                {
                    var imageThumbnail = string.Format("imageThumbnail={0}", pictureUrl);
                    var imageFullsize = string.Format("imageFullsize={0}", pictureUrl);
                    postData += "&" + imageThumbnail.ToString() + "&" + imageFullsize.ToString();
                }
                var data = Encoding.UTF8.GetBytes(postData);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                request.Headers.Add("Authorization", "Bearer " + cToKen);
                using (var stream = request.GetRequestStream()) stream.Write(data, 0, data.Length);
                var response = (HttpWebResponse)request.GetResponse();
                var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            }
            catch (Exception ex)
            {
                Pok_Mess("ส่งข้อความไลน์ไมได้ "+ex.ToString());
            }
        }

        private static void TestPOK()
        {

        }

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
        static void Main(string[] args)
        {
            MyGlobal.cpFlag = ""; MyGlobal.cpWord1 = ""; MyGlobal.cpWord2 = "";
            MyGlobal.cpText1 = ""; MyGlobal.cpText2 = "";MyGlobal.cpMessage = "";
            if (args.Length == 0) { UtilPOKReadMe(); return; } 
            else
            {
                MyGlobal.cpFlag = args[0];
                for (int i = 1; i < args.Length; i++)
                { MyGlobal.cpText1 = MyGlobal.cpText1 + " " + args[i]; }
                for (int i = 2; i < args.Length; i++)
                {
                    MyGlobal.cpText2 = MyGlobal.cpText2 + " " + args[i];
                    MyGlobal.cpMessage = MyGlobal.cpMessage+System.Environment.NewLine+args[i];
                }
            }
            MyGlobal.cpFlag = MyGlobal.cpFlag.Trim().ToUpper();
            MyGlobal.cpText1 = MyGlobal.cpText1.Trim();
            MyGlobal.cpText2 = MyGlobal.cpText2.Trim();
            MyGlobal.cpMessage = MyGlobal.cpMessage.Trim();

            if (MyGlobal.cpText1.Length != 0) { MyGlobal.cpWord1 = args[1].Trim(); }
            if (MyGlobal.cpText2.Length != 0) { MyGlobal.cpWord2 = args[2].Trim(); }
            if (MyGlobal.cpMessage.Length != 0)
            { MyGlobal.cpMessage=System.Environment.NewLine +MyGlobal.cpMessage; }

            SetupVar();
            if (MyGlobal.insource==1)
            {
                Pok_Mess("cpFlag=" + MyGlobal.cpFlag
                    + System.Environment.NewLine + System.Environment.NewLine
                    + ",cpWord1=" + MyGlobal.cpWord1 + ",cpWord2=" + MyGlobal.cpWord2
                    + System.Environment.NewLine + System.Environment.NewLine
                    + ",cpText1=" + MyGlobal.cpText1 + ",cpText2=" + MyGlobal.cpText2);
             }
            switch (MyGlobal.cpFlag)
            {
                case ("T"): TestPOK(); break; 
                case ("?"): UtilPOKReadMe(); break;
                case ("1"): crShortcutPOK(1,@MyGlobal.cpWord1, "", MyGlobal.cpText2,"");break; // สร้างโพลเดอร์ต่างๆ
                case ("2"): crShortcutAuto(0); crTxtAuto(); break; // สร้างโพลเดอร์ต่างๆระบบคลาวด์
                case ("3"):
                    if (MyGlobal.cpWord1 == "0") { RenameNASOH(1, ""); }
                    else
                    {
                        RenameNASOH(1, "");
                        RenameNASOH(1, Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
                        RenameNASOH(0, Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
                        DrvRunPOK(1, @"D:\"); DrvRunPOK(1, @"E:\"); DrvRunPOK(1, @"C:\");
                    }
                    break;
                case ("4"): DelBDTmp(); break;  // ล้างไฟล์ต่างๆในระบบ BDPOS
                case ("5"): LineNotify(MyGlobal.cpWord1, MyGlobal.cpText2); break;
                case ("6"): LineNotify2(MyGlobal.cpWord1, MyGlobal.cpText2); break;  // Pipop
                case ("7"): WinVironment(); break;
            }
       }
   }
}
using System;
using System.IO;
using System.Net;
using Shell32;
using System.Windows.Forms;
using IWshRuntimeLibrary;
using System.Text;
using System.Diagnostics;


namespace Util_pok
{
    static class Program
    {
        public class MyGlobal
        {
            public static int insource = 0;
            public static string cpFlag;
            public static string cpWord1;
            public static string cpWord2;
            public static string cpText1;  // ชุดคำที่ 1 เป็นต้นไป
            public static string cpText2;  // ชุดคำที่ 2 เป็นต้นไป
            public static string cpMessage; // เหมือน  
        }


        private static void UtilPOKReadMe() // Pipop
        {
            MessageBox.Show(
            "util_pok 1 ชื่อไฟล์ ชื่อShortcut  --->สร้างShortcut  " + System.Environment.NewLine
            + "util_pok 2 0 0  --->สร้าง Short-cut จาก backup2c,upnet2c,todos,restart,shutdown ในคลาว์" + System.Environment.NewLine
            + "util_pok 3 0 0  --->ล้างไฟล์ nasoh เฉพาะโพลเดอร์" + System.Environment.NewLine
            + "util_pok 3 1 0  --->ทั้งหมด" + System.Environment.NewLine
            + "util_pok 4 0 0  --->ล้างไฟล์ขยะของ D:\\Bdpos\\ และ D:\\BdposVFP\\" + System.Environment.NewLine
            + "util_pok 5 T M  --->Line Notify  M=* ขึ้นบรรทัดใหม่" + System.Environment.NewLine
            + "util_pok 6 T M+0+0+Picture " + System.Environment.NewLine
            + "             --->Line Notify Token ข้อความ+stickerPackageID+stickerID+Picture"+System.Environment.NewLine
            + "             เช่น util_pok 6 Token Pipop*Youyod+0+0+http://uyod.net/logo.jpg" + System.Environment.NewLine
            + "             เช่น util_pok 6 Token Pipop*Youyod+0+0+" + System.Environment.NewLine
            + "util_pok 7 0 0  --->สภาพแวดล้อมของวินโดร์" + System.Environment.NewLine
            + "util_pok 8 0 0  --->32 หรือ 64 บิต (w32.bit,w64.bit)" + System.Environment.NewLine
            );
        }

        static void Pok_Mess(string cfWord)
        {
            if (MyGlobal.insource==0) { return; }
            MessageBox.Show(cfWord + System.Environment.NewLine
            + "" + System.Environment.NewLine
            + "" + System.Environment.NewLine
            , "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }


        private static void SetupVar()
        {
            if (System.IO.Directory.Exists(@"J:\Entertain\Movie\Private")) { MyGlobal.insource = 1; }
        }


        private static void crShortcutPOK(int nFlag,string TargetFile,string SubfixFile, string ShortcutName,string IconFileRun)
        {
            ShortcutName = ShortcutName.Trim();
            IconFileRun = IconFileRun.Trim();
            TargetFile = TargetFile.Trim();
            string ShortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // Desktop
            string IconFile= TargetFile.Substring(0, TargetFile.Length - 4) + ".ico";

            switch (nFlag)
            {
                case (1):
                    break;
                case (2):
                    TargetFile = System.IO.Directory.GetCurrentDirectory()+"\\"+TargetFile;
                    IconFile = System.IO.Directory.GetCurrentDirectory() + @"\UyodTeam\Ico\"+IconFile;
                    break;
            }

            // nFlag = 2 ถ้าไม่เจอไฟล์ไอคอนให้ออกไปก่อน 
            if (nFlag == 2 && !System.IO.File.Exists(IconFile)) { return; }
            
            // ส่งไฟล์Iconมาด้วยให้เอาไฟล์นั้นใช้แทน
            if (IconFileRun != "" && System.IO.File.Exists(IconFileRun)) { IconFile = IconFileRun; }

            if (MyGlobal.insource==1)
            {
                Pok_Mess("ShortcutName = " + ShortcutName 
                + System.Environment.NewLine + " , TargetFile = " + TargetFile
                +System.Environment.NewLine + " , IconFile = " + IconFile
                + System.Environment.NewLine + " , ShortcutPath = " + ShortcutPath
                );
            }
            // ลบไอคอนถ้ามีไฟล์หายไป
            if (!System.IO.File.Exists(TargetFile) && System.IO.File.Exists(ShortcutPath + "\\" + ShortcutName + ".lnk"))
            { System.IO.File.Delete(ShortcutPath + "\\" + ShortcutName + ".lnk"); return; }

            if (!System.IO.File.Exists(TargetFile)) { Pok_Mess("ไม่พบไฟล์ "+TargetFile); return; }
            if (System.IO.File.Exists(ShortcutPath+"\\"+ShortcutName + ".lnk")) { return; }

            string ShortcutLocation = System.IO.Path.Combine(ShortcutPath, ShortcutName + ".lnk");
            WshShell shell = new WshShell();
            IWshShortcut crShortcut = (IWshShortcut)shell.CreateShortcut(ShortcutLocation);
            // โพลเดอร์ไฟล์ที่ทำงาน 
            crShortcut.WorkingDirectory = System.IO.Directory.GetCurrentDirectory();
            // อธิบายรายละเอียด
            crShortcut.Description = "สร้างโดย C# UyodTeam";   // Comment ใน Shortcut
            if (System.IO.File.Exists(IconFile))
            {
                crShortcut.IconLocation = IconFile ;   // icon ที่แสดงใน Desktop
            }
            if (SubfixFile != "") { TargetFile = TargetFile + " " + SubfixFile.Trim(); }
            crShortcut.TargetPath = TargetFile;         // 
            crShortcut.Save();
        }


        private static void crWindowsIcon()
        {
            string ShortcutName = "เข้าสู่ DOS";
            string ShortcutLocation = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), ShortcutName + ".lnk");
            WshShell shell = new WshShell();
            IWshShortcut crShortcut = (IWshShortcut)shell.CreateShortcut(ShortcutLocation);
            crShortcut.Description = "สร้างโดย C# UyodTeam";   // Comment ใน Shortcut
            crShortcut.IconLocation = System.IO.Directory.GetCurrentDirectory()+ "\\UyodTeam\\Ico\\todos.ico";   // icon ที่แสดงใน Desktop
            crShortcut.TargetPath = @"C:\Windows\System32\cmd.exe";
            crShortcut.WorkingDirectory = @"D:\Bdpos";
            crShortcut.Save();
        }


        private static void DownLoadIco()
        {
            string FileOnUyod = "http://www.uyod.net/ico/";
            string SaveToFolder = @"UyodTeam\Ico\";
            string SaveToFile = "";
            WebClient myWebClient = new WebClient();

            SaveToFile = "backup2c.ico";  
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "backup2c2.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "card.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "desktop-1.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "desktop-2.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "fix.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "filetran.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "food-1.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "food-2.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "food-3.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "food-4.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "food-5.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "hd.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "ho2.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "home.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "home2.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "hotel-1.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "hotel-2.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "key-1.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "key-2.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "key-3.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "man-1.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "man-2.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "poswin.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "traffic-1.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "upnet2c.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "todos.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "todos.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "print.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "restart.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            SaveToFile = "shutdown.ico";
            if (!System.IO.File.Exists(SaveToFolder + SaveToFile))
            { myWebClient.DownloadFile(FileOnUyod + SaveToFile, SaveToFolder + SaveToFile); }
            myWebClient.Dispose();
        }


        private static void crShortcutAuto(int nCr) // เสริมระบบ backup2c.bat
        {
            if (!System.IO.Directory.Exists(@"UyodTeam\Ico")) { System.IO.Directory.CreateDirectory(@"UyodTeam\Ico");}
            if (!System.IO.File.Exists(@"UyodTeam\Ico\shutdown.ico")) { DownLoadIco(); }
            if (System.IO.File.Exists("backup2c.bat")) { crShortcutPOK(2, "backup2c.bat","", "สำรองข้อมูลทุกวัน",""); }
            if (System.IO.File.Exists("upnet2c.bat")) { crShortcutPOK(2, "upnet2c.bat", "", "ปรับปรุงผ่านคลาวด์",""); }
            if (System.IO.File.Exists("todos.bat")) { crShortcutPOK(2, "todos.bat", "", "เข้า DOS",""); }
            if (System.IO.File.Exists("restart.bat")) { crShortcutPOK(2, "restart.bat", "", "รีสตาร์ทเครื่อง", ""); }
            if (System.IO.File.Exists("shutdown.bat")) { crShortcutPOK(2, "shutdown.bat", "", "ปิดเครื่อง", ""); }
            switch (nCr)
            {
                case (1):  break;
            }
        }

        private static void crTxtAuto()
        {
            string bdPath=@"D:\BdPos";
            if (!System.IO.Directory.Exists(bdPath)) { bdPath = @"C:\BdPos"; }
            if (!System.IO.Directory.Exists(bdPath)) { return; }
            bdPath = bdPath+@"\repoPOK.txt";
            if (System.IO.Directory.GetCurrentDirectory().Substring(0, 2) != "C:")
            {
                TextWriter Print2Txt = new StreamWriter(bdPath);
                Print2Txt.WriteLine(System.IO.Directory.GetCurrentDirectory()+"\\");
                Print2Txt.Close();
            }
        }

        private static void RenameNASOH(int nRename,string cfFoder)
        {
            cfFoder = cfFoder.Trim();
            if (cfFoder == "") { cfFoder = Directory.GetCurrentDirectory(); }
            if (!Directory.Exists(cfFoder)) { return; }
            DirectoryInfo dFolder=new DirectoryInfo(cfFoder);
            FileInfo[] fFolder;
            if (nRename == 0) { fFolder = dFolder.GetFiles("*.nasoh", SearchOption.TopDirectoryOnly); }
            else { fFolder = dFolder.GetFiles("*.nasoh", SearchOption.AllDirectories); }
            string cfSource,cflRnaos;
            foreach (FileInfo lFolder in fFolder)
            {
                cfSource = lFolder.FullName;
                cflRnaos = lFolder.FullName.Substring(0, lFolder.FullName.Length - 6);
                Console.WriteLine(cfSource);
                if (System.IO.File.Exists(cflRnaos)) { System.IO.File.Delete(cfSource); }
                else { System.IO.File.Move(cfSource, cflRnaos); }
            }           
        }


        private static void DrvRunPOK(int nFunction,string cDRV)
        {
            // Sample cDRV=@"C:\"
            if (!System.IO.Directory.Exists(cDRV)) { Pok_Mess("ไม่มี Drive "+cDRV); return; }
            RenameNASOH(0, cDRV);
            string cFolderCut;
            foreach (string sInFolder in Directory.GetDirectories(cDRV))
            {
                cFolderCut = sInFolder.Substring(3, 1);
                switch (cFolderCut)
                {
                    case ("$"):
                    case ("C"):
                    case ("D"):
                    case ("P"):
                    case ("R"):
                    case ("U"):
                    case ("W"):
                    case ("S"):
                        break;
                    default:
                        switch (nFunction)
                        {
                            case (1): RenameNASOH(1, sInFolder);break;
                        }
                        break;
                }
            }
        }


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


        //  ลบไฟล์ต่างๆของ D:\BDPOS\ 
        private static void DelBDTmp()
        {
            string drvCloud = Environment.GetEnvironmentVariable("CLOUD_DRIVE");
            if (drvCloud != null && drvCloud != "")
            {
                DelBDTmpFle(drvCloud + "Backup", "*.zip", 30);
                DelBDTmpFle(drvCloud + "Backup", "*.rar", 30);
                DelBDTmpFle(drvCloud + "pdf", "*.pdf", 30);
                DelBDTmpFle(drvCloud + "Screen", "*.jpg", 30);
                DelBDTmpFle(drvCloud + "UyodTeam", "*.zip", 30);
                DelBDTmpFle(drvCloud + "UyodTeam", "*.rar", 30);
                DelBDTmpFle(drvCloud + "txt", "*.txt", 30);
                DelBDTmpFle(drvCloud + "txt", "*.bak", 30);
                DelBDTmpFle(@"C:\Temp", "*.*", 3);
            }
            // สำหรับ D:\BdPos
            DelBDTmpFle(@"D:\BdPos\Bakdata", "F*.DBF", 7);
            DelBDTmpFle(@"D:\BdPos\Bakdata", "F*.CDX", 7);
            DelBDTmpFle(@"D:\BdPos\Bakdata", "C*.DBF", 7);
            DelBDTmpFle(@"D:\BdPos\Bakdata", "C*.CDX", 7);
            DelBDTmpFle(@"D:\BdPos\Bakdata", "F*.TXT", 7);
            DelBDTmpFle(@"D:\BdPos\Temp", "*.DBF", 0);
            DelBDTmpFle(@"D:\BdPos\Temp", "*.TMP", 0);
            DelBDTmpFle(@"D:\BdPos\Temp", "*.CDX", 0);
            DelBDTmpFle(@"D:\BdPos\Error", "*.DBF", 7);
            DelBDTmpFle(@"D:\BdPos\Error", "*.CDX", 7);
            DelBDTmpFle(@"D:\BdPos\Error", "*.ERR", 7);
            DelBDTmpFle(@"D:\BdPos\_Error", "*.DBF", 7);
            DelBDTmpFle(@"D:\BdPos\_Error", "*.CDX", 7);
            DelBDTmpFle(@"D:\BdPos\_Error", "*.ERR", 7);
            DelBDTmpFle(@"D:\BdPos\DayInOut", "*.DBF", 7);
            DelBDTmpFle(@"D:\BdPos\DayInOut", "*.CDX", 7);
            DelBDTmpFle(@"D:\Temp", "*.*",30);
            DelBDTmpFle(@"D:\BdPos\HO2MACH", "*.txt", 3);
            // สำหรับ D:\BdPosVFP
            DelBDTmpFle(@"D:\BdPosVFP\Bakdata", "F*.DBF", 7);
            DelBDTmpFle(@"D:\BdPosVFP\Bakdata", "F*.CDX", 7);
            DelBDTmpFle(@"D:\BdPosVFP\Bakdata", "C*.DBF", 7);
            DelBDTmpFle(@"D:\BdPosVFP\Bakdata", "C*.CDX", 7);
            DelBDTmpFle(@"D:\BdPosVFP\Bakdata", "F*.TXT", 7);
            DelBDTmpFle(@"D:\BdPosVFP\Temp", "*.DBF", 0);
            DelBDTmpFle(@"D:\BdPosVFP\Temp", "*.TMP", 0);
            DelBDTmpFle(@"D:\BdPosVFP\Temp", "*.CDX", 0);
            DelBDTmpFle(@"D:\BdPosVFP\Error", "*.DBF", 7);
            DelBDTmpFle(@"D:\BdPosVFP\Error", "*.CDX", 7);
            DelBDTmpFle(@"D:\BdPosVFP\Error", "*.ERR", 7);
            DelBDTmpFle(@"D:\BdPosVFP\_Error", "*.DBF", 7);
            DelBDTmpFle(@"D:\BdPosVFP\_Error", "*.CDX", 7);
            DelBDTmpFle(@"D:\BdPosVFP\_Error", "*.ERR", 7);
            DelBDTmpFle(@"D:\BdPosVFP\DayInOut", "*.DBF", 7);
            DelBDTmpFle(@"D:\BdPosVFP\DayInOut", "*.CDX", 7);
            DelBDTmpFle(@"D:\BdPosVFP\HO2MACH", "*.txt", 3);
            DelBDTmpFle(@"E:\PDF Backup", "PDF*.pdf", 7);
            DelBDTmpFle(@"E:\Temp", "*.*", 3);
            DelBDTmpFle(@"M:\HO2MACH", "*.txt", 3);
        }


        static void OpenWebSite(String URLname)
        {
            var opentwebthis = new ProcessStartInfo("chrome.exe");  // iexplore.exe
            opentwebthis.Arguments = URLname;
            Process.Start(opentwebthis);
        }


        private static void LineNotify(string cToKen,string LineMessage)  
        {
            LineMessage = Text2Replace(LineMessage);
            if (MyGlobal.insource == 1)
            {
                Pok_Mess("ข้อความเตือนทางไลน์ " + System.Environment.NewLine + System.Environment.NewLine
                   + "ToKen : " + cToKen + System.Environment.NewLine + ",Message : " + LineMessage);
            }
            if (cToKen=="")
            { Pok_Mess("LineNotify Error: ค่า Token เป็นช่องว่าง");
                OpenWebSite("https://notify-bot.line.me/my/");
                return;
            } 
            if (LineMessage == "") {Pok_Mess("LineNotify Error: ข้อความ เป็นช่องว่าง");  return; }

            var request = (HttpWebRequest)WebRequest.Create("https://notify-api.line.me/api/notify");
            var postData = string.Format("message={0}",LineMessage);
            var data = Encoding.UTF8.GetBytes(postData);

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            request.Headers.Add("Authorization", "Bearer " + cToKen);

            using (var stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }
            var response = (HttpWebResponse)request.GetResponse();
            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
        }

        // เปลี่ยน * เป็น ขึ้นบรรทัดใหม่  Pipop
        private static string Text2Replace(string mText) 
        {
            string cWordTake, cWordFor;
            mText = mText.Trim();
            cWordFor = "";
            for (int i = 0; i < mText.Length; i++)
            {
                cWordTake = mText.Substring(i, 1);
                switch (cWordTake)
                {
                    case ("*"):
                        cWordFor = cWordFor + System.Environment.NewLine; break;
                    default:
                        cWordFor = cWordFor + cWordTake; break;
                }
            }
            return cWordFor;
        }

        private static void WinVironment()
        {
            // Check Public IP
            string url = "http://checkip.dyndns.org";
            System.Net.WebRequest req = System.Net.WebRequest.Create(url);
            System.Net.WebResponse resp = req.GetResponse();
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            string response = sr.ReadToEnd().Trim();
            string[] a = response.Split(':');
            string a2 = a[1].Substring(1);
            string[] a3 = a2.Split('<');
            string ipGlobal = a3[0];

            TextWriter WriteTxt = new StreamWriter("Environment.txt");
            WriteTxt.WriteLine("Username     : {0}", Environment.UserName);
            WriteTxt.WriteLine("Computername : {0}", Environment.MachineName);
            WriteTxt.WriteLine("Domain       : {0}", Environment.UserDomainName);
            WriteTxt.WriteLine("Directory    : {0}", Environment.CurrentDirectory);
            WriteTxt.WriteLine("OS(64BIT)    : {0}", Environment.Is64BitOperatingSystem);
            WriteTxt.WriteLine("Machine      : {0}", Environment.MachineName);
            WriteTxt.WriteLine("OS           : {0}", Environment.OSVersion);
            WriteTxt.WriteLine("Public IP    : {0}", ipGlobal);
            WriteTxt.Close();
        }

        // Pipop
        private static void LineNotify2(string cToKen,string cLineWordSet)
        {
            string LineMessage = "";
            int stickerPackageID = 0;
            int stickerID = 0;
            string pictureUrl = "";
            // แยกคำจาก LineMessage
            string cWordTake, cWordFor;
            cLineWordSet = cLineWordSet.Trim();
            cWordFor = "";
            int j = 0;
            for (int i = 0; i < cLineWordSet.Length; i++)
            {
                cWordTake = cLineWordSet.Substring(i, 1);
                switch (cWordTake)
                {
                    case ("+"):
                        j++;
                        switch (j)
                        {
                            case (1): LineMessage = cWordFor; break;
                            case (2): stickerPackageID = int.Parse(cWordFor)  ; break;  
                            case (3): stickerID = int.Parse(cWordFor); break;
                        }
                        cWordFor = "";
                        break;
                    case ("*"):
                        cWordFor = cWordFor + System.Environment.NewLine; break;
                    default:
                        cWordFor = cWordFor + cWordTake; break;
                }
            }
            //
            pictureUrl = cWordFor;// อันสุดท้าย

            LineMessage = Text2Replace(LineMessage);
            if (MyGlobal.insource == 1)
            {
                Pok_Mess("ข้อความเตือนทางไลน์ " + System.Environment.NewLine + System.Environment.NewLine
                +"cToKen :" + cToKen + System.Environment.NewLine
                + ",LineMessage :" + LineMessage + System.Environment.NewLine
                + ",stickerPackageID :" + stickerPackageID.ToString() + System.Environment.NewLine
                + ",stickerID :" + stickerID.ToString() + System.Environment.NewLine
                + ",pictureUrl :" + pictureUrl);
            }
            if (cToKen == "")
            {
                Pok_Mess("LineNotify Error: ค่า Token เป็นช่องว่าง");
                OpenWebSite("https://notify-bot.line.me/my/");
                return;
            }
            try
            {
                var request = (HttpWebRequest)WebRequest.Create("https://notify-api.line.me/api/notify");
                var postData = string.Format("message={0}", LineMessage);
                if (stickerPackageID > 0 && stickerID > 0)
                {
                    var stickerPackageId = string.Format("stickerPackageId={0}", stickerPackageID);
                    var stickerId = string.Format("stickerId={0}", stickerID);
                    postData += "&" + stickerPackageId.ToString() + "&" + stickerId.ToString();
                }
                if (pictureUrl != "")
                {
                    var imageThumbnail = string.Format("imageThumbnail={0}", pictureUrl);
                    var imageFullsize = string.Format("imageFullsize={0}", pictureUrl);
                    postData += "&" + imageThumbnail.ToString() + "&" + imageFullsize.ToString();
                }
                var data = Encoding.UTF8.GetBytes(postData);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                request.Headers.Add("Authorization", "Bearer " + cToKen);
                using (var stream = request.GetRequestStream()) stream.Write(data, 0, data.Length);
                var response = (HttpWebResponse)request.GetResponse();
                var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            }
            catch (Exception ex)
            {
                Pok_Mess("ส่งข้อความไลน์ไมได้ "+ex.ToString());
            }
        }


        private static void Win32Or64BIT()
        {
            if (!System.IO.File.Exists("Dummy.txt"))
            {
                TextWriter WriteTxt = new StreamWriter("Dummy.txt");
                WriteTxt.WriteLine("32or64");
                WriteTxt.Close();
            }
            if (Environment.Is64BitOperatingSystem) // เป็นวินโดร์ 64 บิต 
            {
                if (!System.IO.File.Exists("w64.bit")) { System.IO.File.Copy("Dummy.txt","w64.bit"); }
                if (System.IO.File.Exists("w32.bit")) { System.IO.File.Delete("w32.bit"); }
            }
            else // เป็นวินโดร์ 32 บิต 
            {
                if (!System.IO.File.Exists("w32.bit")) { System.IO.File.Copy("Dummy.txt", "w32.bit"); }
                if (System.IO.File.Exists("w64.bit")) { System.IO.File.Delete("w64.bit"); }
            }
        }


        private static void TestPOK()
        {

        }

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
        static void Main(string[] args)
        {
            MyGlobal.cpFlag = ""; MyGlobal.cpWord1 = ""; MyGlobal.cpWord2 = "";
            MyGlobal.cpText1 = ""; MyGlobal.cpText2 = "";MyGlobal.cpMessage = "";
            if (args.Length == 0) { UtilPOKReadMe(); return; } 
            else
            {
                MyGlobal.cpFlag = args[0];
                for (int i = 1; i < args.Length; i++)
                { MyGlobal.cpText1 = MyGlobal.cpText1 + " " + args[i]; }
                for (int i = 2; i < args.Length; i++)
                {
                    MyGlobal.cpText2 = MyGlobal.cpText2 + " " + args[i];
                    MyGlobal.cpMessage = MyGlobal.cpMessage+System.Environment.NewLine+args[i];
                }
            }
            MyGlobal.cpFlag = MyGlobal.cpFlag.Trim().ToUpper();
            MyGlobal.cpText1 = MyGlobal.cpText1.Trim();
            MyGlobal.cpText2 = MyGlobal.cpText2.Trim();
            MyGlobal.cpMessage = MyGlobal.cpMessage.Trim();

            if (MyGlobal.cpText1.Length != 0) { MyGlobal.cpWord1 = args[1].Trim(); }
            if (MyGlobal.cpText2.Length != 0) { MyGlobal.cpWord2 = args[2].Trim(); }
            if (MyGlobal.cpMessage.Length != 0)
            { MyGlobal.cpMessage=System.Environment.NewLine +MyGlobal.cpMessage; }

            SetupVar();
            if (MyGlobal.insource==1)
            {
                Pok_Mess("cpFlag=" + MyGlobal.cpFlag
                    + System.Environment.NewLine + System.Environment.NewLine
                    + ",cpWord1=" + MyGlobal.cpWord1 + ",cpWord2=" + MyGlobal.cpWord2
                    + System.Environment.NewLine + System.Environment.NewLine
                    + ",cpText1=" + MyGlobal.cpText1 + ",cpText2=" + MyGlobal.cpText2);
             }
            switch (MyGlobal.cpFlag)
            {
                case ("T"): TestPOK(); break; 
                case ("?"): UtilPOKReadMe(); break;
                case ("1"): crShortcutPOK(1,@MyGlobal.cpWord1, "", MyGlobal.cpText2,"");break; // สร้างโพลเดอร์ต่างๆ
                case ("2"): crShortcutAuto(0); crTxtAuto(); break; // สร้างโพลเดอร์ต่างๆระบบคลาวด์
                case ("3"):
                    if (MyGlobal.cpWord1 == "0") { RenameNASOH(1, ""); }
                    else
                    {
                        RenameNASOH(1, "");
                        RenameNASOH(1, Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
                        RenameNASOH(0, Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
                        DrvRunPOK(1, @"D:\"); DrvRunPOK(1, @"E:\"); DrvRunPOK(1, @"C:\");
                    }
                    break;
                case ("4"): DelBDTmp(); break;  // ล้างไฟล์ต่างๆในระบบ BDPOS
                case ("5"): LineNotify(MyGlobal.cpWord1, MyGlobal.cpText2); break;
                case ("6"): LineNotify2(MyGlobal.cpWord1, MyGlobal.cpText2); break;  // Pipop
                case ("7"): WinVironment(); break; // สภาพแวดล้อมวินโดร์
                case ("8"): Win32Or64BIT(); break; // ตรวจว่าเป็น 32 หรือ 64 BIT
            }
       }
   }
}
