Example #1
0
 //構造函數
 public FTPcontrol(string ftpServerIP, string ftpUserID, string ftpPassword, frmProgress _myForm)
 {
     this.ftpServerIP =  ftpServerIP;
     this.ftpUserID = ftpUserID;
     this.ftpPassword = ftpPassword;
     this.myForm = _myForm;
 }
Example #2
0
        /// <summary>
        /// 下載新程式
        /// </summary>
        /// <param name="FTPSever">FTP服務器ip或機器名</param>
        /// <param name="VirDir">虛擬目錄</param>
        /// <param name="RootPath">調用新程式的主目錄</param>
        /// <param name="FilePath">程式所在相對路徑</param>
        /// <param name="FtpProgramPath">FTP服務器上此程式的路徑</param>
        /// <param name="User">用戶</param>
        /// <param name="Password">密碼</param>
        public static void DownloadNewProject(string FTPSever, string VirDir, string RootPath, string FilePath, string FtpProgramPath, string User, string Password)
        {
            string[] folders = FilePath.Split('\\');
            string temp = "";
            string localFilePath = "";

            #region ================= 檢查本機目錄是否存在.沒有則建立 =================

            for (int i = 0; i < folders.Length - 1; i++)
            {
                temp = temp + "\\" + folders[i];
                if (!Directory.Exists(RootPath + temp))
                {
                    Directory.CreateDirectory(RootPath + temp);
                }
            }
            if (Directory.Exists(RootPath + temp + @"\TEMP"))
            {
                //刪除臨時文件夾
                DeleteFolder(RootPath + temp + @"\TEMP");
            }
            //建立臨時文件夾
            Directory.CreateDirectory(RootPath + temp + @"\TEMP");

            #endregion

            #region ================= 下載清單文件 =================

            // 得到FTP路徑
            temp = FtpProgramPath;
            localFilePath = FilePath.Substring(0, FilePath.LastIndexOf("\\"));
            //下載更新文件
            frmProgress myProgress = new frmProgress();
            string FullPath = FTPSever + "/" + VirDir + "/" + temp;
            FTPcontrol myFTP;
            try
            {
                myFTP = new FTPcontrol(FullPath, User, Password, myProgress);
                try
                {
                    //下載遠程更新文件單清
                    myFTP.Download(FullPath + "/Manifest.ini", RootPath + "\\" + localFilePath + @"\TEMP\", "Manifest.ini");
                }
                catch
                {
                    return;
                }
            }
            catch
            {
                return;
            }

            #endregion

            #region ================= 讀取清單文件,根據路徑下載 =================
            string UpdatePath;
            string DownloadFileSavePath, NewDirectory;
            string[] DirectoryList;
            string CurDir = "";
            IniFile ini = new IniFile(RootPath + "\\" + localFilePath + "\\TEMP\\Manifest.ini");
            string[] Manifest_KeyName = ini.GetKeyNames("UpdateRootPath");
            if (Manifest_KeyName.Length > 0)
            {
                UpdatePath = ini.GetString("UpdateRootPath", Manifest_KeyName[0], "");
                //刪除清單
                File.Delete(RootPath + "\\" + localFilePath + "\\TEMP\\Manifest.ini");
                //刪除臨時文件夾
                DeleteFolder(RootPath + "\\" + localFilePath + "\\TEMP");

                //獲得更新服務器上所有文件信息
                //文件信息放在myFTP.DownloadFileList里
                myFTP.GetFileList(FTPSever + "/" + VirDir + "/" + UpdatePath);

                //顯示更新提示窗體
                myProgress.Show();

                #region =============== 顯示下載文件明細到提示窗體 ===============
                for (int j = 0; j < myFTP.DownloadFileList.Count; j++)
                {
                    string[] arrSubItem = new string[4];
                    arrSubItem[0] = myFTP.DownloadFileList[j].FileName;
                    arrSubItem[1] = myFTP.DownloadFileList[j].File_Size_Byte;
                    arrSubItem[2] = "0";
                    arrSubItem[3] = "Wait";

                    ListViewItem LiItem = new ListViewItem(arrSubItem, 0);
                    myProgress.listView_UpdateList.Items.Insert(j, LiItem);
                }
                #endregion

                #region ================= 開始下載 =================
                try
                {
                    for (int j = 0; j < myFTP.DownloadFileList.Count; j++)
                    {
                        CurDir = "";
                        //文件不存在,直接下載
                        DownloadFileSavePath = RootPath + "\\" + localFilePath + @"\" + myFTP.DownloadFileList[j].ClientPath.Substring(0, myFTP.DownloadFileList[j].ClientPath.IndexOf(myFTP.DownloadFileList[j].FileName));
                        NewDirectory = myFTP.DownloadFileList[j].ClientPath.Substring(0, myFTP.DownloadFileList[j].ClientPath.IndexOf(myFTP.DownloadFileList[j].FileName));
                        DirectoryList = NewDirectory.Split('\\');
                        for (int x = 0; x < DirectoryList.Length; x++)
                        {

                            if (DirectoryList[x] != "")
                            {
                                CurDir = CurDir + @"\" + DirectoryList[x];
                                if (!Directory.Exists(RootPath + "\\" + localFilePath + CurDir))
                                {
                                    //如何文件夾不存在,則建立.
                                    Directory.CreateDirectory(RootPath + "\\" + localFilePath + CurDir);
                                }
                            }
                        }
                        //開始下載
                        myFTP.Download(myFTP.DownloadFileList[j].FullPath,
                                       DownloadFileSavePath,
                                       myFTP.DownloadFileList[j].FileName,
                                       myFTP.DownloadFileList[j].File_Size,
                                       myFTP.DownloadFileList[j].File_Size_Byte,
                                       j);
                        FileInfo tmp = new FileInfo(DownloadFileSavePath + @"\" + myFTP.DownloadFileList[j].FileName);
                        tmp.LastWriteTime = DateTime.ParseExact(myFTP.DownloadFileList[j].Modified_DateTime, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                myProgress.CloseProgress();
                #endregion
            }

            #endregion
        }
Example #3
0
        /// <summary>
        /// 檢查並更新指定程式
        /// </summary>
        /// <param name="FTPSever">更新服務器IP或機器名</param>
        /// <param name="VirDir">虛擬目錄</param>
        /// <param name="RootPath">主界面目錄</param>
        /// <param name="FilePath">程式所在相對路徑及文件名</param>
        /// <param name="FtpProgramPath">FTP服務器上此程式的路徑</param>
        /// <param name="User">訪問用戶名</param>
        /// <param name="Password">訪問密碼</param>
        /// <returns>成功則返回"-1".失敗則返回異常說明字符串</returns>
        public static string CheckForUpdate(string FTPSever, string VirDir, string RootPath, string FilePath, string FtpProgramPath, string User, string Password)
        {
            string[] Manifest_KeyName;
            string UpdatePath;
            string GUID;
            string FullPath;
            string FileModifiedDate;
            string DownloadFileSavePath;
            string NewDirectory;
            string[] DirectoryList;
            string CurDir = "";
            string[] BatContext = new string[3];
            frmProgress myProgress = new frmProgress();

            #region =============== 1.得到本程式的更新路徑及GUID ===============
            string localFilePath = FilePath.Substring(0, FilePath.LastIndexOf("\\"));
            IniFile ini = new IniFile(RootPath + "\\" + localFilePath + "\\Manifest.ini");

            /////////////////////////////////////////////////////////////////////////////////////////////
            // 程式更新路徑暫時由本地Manifest.ini里的UpdateRootPath,改為直接用IEMENU.ini里的路徑,      //
            // 以免造成挂程式時UpdateRootPath設置錯誤后,系統永遠都會按錯誤路徑去下載更新,造成無法      //
            // 預知的錯誤.(IEMENU.ini的路徑設置錯誤的機率低,且設置錯誤后,子程式無法動行,也起到防呆功能)//
            /////////////////////////////////////////////////////////////////////////////////////////////

            UpdatePath = FtpProgramPath;
            Manifest_KeyName = ini.GetKeyNames("GUID");
            if (Manifest_KeyName.Length > 0)
            {
                GUID = ini.GetString("GUID", Manifest_KeyName[0], "");
            }
            else
            {
                //Added by Donnie on 2012/10/11
                //找不到Manifest.ini則重新下載
                // 無法找到更新清單.拋出異常
                //return ("Can not find the \"Manifest.ini\"");
                GUID = "NULL";
            }

            #endregion

            #region =============== 2.下載更新服務器上的Manifest.ini ===============

            if (Directory.Exists(RootPath + "\\" + localFilePath + @"\TEMP"))
            {
                //刪除臨時文件夾
                DeleteFolder(RootPath + "\\" + localFilePath + @"\TEMP");
            }
            if (File.Exists(RootPath + "\\" + localFilePath + "\\TEMP\\Manifest.ini"))
            {
                File.Delete(RootPath + "\\" + localFilePath + "\\TEMP\\Manifest.ini");
            }
            //建立臨時文件夾
            Directory.CreateDirectory(RootPath + "\\" + localFilePath + @"\TEMP");

            //下載更新文件
            FullPath = FTPSever + "/" + VirDir + "/" + UpdatePath;
            FTPcontrol myFTP;
            try
            {
                myFTP = new FTPcontrol(FullPath, User, Password, myProgress);
                try
                {
                    //下載遠程更新文件單清
                    myFTP.Download(FullPath + "/Manifest.ini", RootPath + "\\" + localFilePath + @"\TEMP\", "Manifest.ini");
                }
                catch
                {
                    return ("Failed to Download the Manifest.ini!");
                }
            }
            catch
            {
                return ("Can not connect the Server!");
            }

            #endregion

            #region =============== 3.比較Manifest.ini的GUID值,判斷是否更新 ===============

            ini = new IniFile(RootPath + "\\" + localFilePath + "\\TEMP\\Manifest.ini");
            Manifest_KeyName = ini.GetKeyNames("GUID");
            if (GUID != ini.GetString("GUID", Manifest_KeyName[0], ""))
            {
                // 檢查是否當前程式已經打開,如果打開則不更新(否則會出現此程式已被佔用錯誤)
                try
                {
                    string filename = Path.GetFileNameWithoutExtension(FilePath);
                    Process[] ps = Process.GetProcessesByName(filename);
                    foreach (Process p in ps)
                    {
                        return (p.MainWindowTitle + " 自動更新提示:" + "\r\n\r\n" +
                                "系統檢測到本程式有新版本可更新, 但已有" + ps.Length.ToString() + "個 (" + p.MainWindowTitle + ") 程式正在運行,請先退出再執行下載更新操作!" +
                                "\r\n" +
                                "The program (" + p.MainWindowTitle + ") is running,please close it and try again.");
                    }
                }
                catch
                {
                    //Do Nothing
                }
                #region =============== 4.開始更新 ===============

                //顯示更新提示窗體
                myProgress.Show();

                //獲得更新服務器上所有文件信息
                //文件信息放在myFTP.DownloadFileList里
                myFTP.GetFileList(FullPath);

                #region =============== 去掉不用更新的文件 ===============

                for (int j = 0; j < myFTP.DownloadFileList.Count; j++)
                {
                    if (File.Exists(RootPath + "\\" + localFilePath + @"\" + myFTP.DownloadFileList[j].ClientPath))
                    {
                        //文件存在,比較兩個文件的修改日期+時間是否一致.
                        //如果一致則刪除.
                        FileInfo dirFiles = new FileInfo(RootPath + "\\" + localFilePath + @"\" + myFTP.DownloadFileList[j].ClientPath);
                        //格式: 年年年年月月日日時時分分
                        FileModifiedDate = String.Format("{0:D4}", dirFiles.LastWriteTime.Year) +
                                           String.Format("{0:D2}", dirFiles.LastWriteTime.Month) +
                                           String.Format("{0:D2}", dirFiles.LastWriteTime.Day) +
                                           String.Format("{0:D2}", dirFiles.LastWriteTime.Hour) +
                                           String.Format("{0:D2}", dirFiles.LastWriteTime.Minute);
                        if (FileModifiedDate == myFTP.DownloadFileList[j].Modified_DateTime)
                        {
                            myFTP.DownloadFileList.RemoveAt(j);
                        }
                    }
                }

                #endregion

                #region =============== 顯示下載文件明細到提示窗體 ===============
                for (int j = 0; j < myFTP.DownloadFileList.Count; j++)
                {
                    string[] arrSubItem = new string[4];
                    arrSubItem[0] = myFTP.DownloadFileList[j].FileName;
                    arrSubItem[1] = myFTP.DownloadFileList[j].File_Size_Byte;
                    arrSubItem[2] = "0";
                    arrSubItem[3] = "Wait";
                    ListViewItem LiItem = new ListViewItem(arrSubItem, 0);
                    myProgress.listView_UpdateList.Items.Insert(j, LiItem);
                }
                #endregion

                #region =============== 根據下載清單開始下載文件 ===============

                for (int j = 0; j < myFTP.DownloadFileList.Count; j++)
                {
                    if (File.Exists(RootPath + "\\" + localFilePath + @"\" + myFTP.DownloadFileList[j].ClientPath))
                    {
                        //文件存在,比較兩個文件的修改日期+時間是否一致.
                        //如果不一致才更新.
                        FileInfo dirFiles = new FileInfo(RootPath + "\\" + localFilePath + @"\" + myFTP.DownloadFileList[j].ClientPath);
                        //格式: 年年年年月月日日時時分分
                        FileModifiedDate = String.Format("{0:D4}", dirFiles.LastWriteTime.Year) +
                                           String.Format("{0:D2}", dirFiles.LastWriteTime.Month) +
                                           String.Format("{0:D2}", dirFiles.LastWriteTime.Day) +
                                           String.Format("{0:D2}", dirFiles.LastWriteTime.Hour) +
                                           String.Format("{0:D2}", dirFiles.LastWriteTime.Minute);
                        if (FileModifiedDate != myFTP.DownloadFileList[j].Modified_DateTime)
                        {
                            // 開始更新

                            // 將文件下載到 TEMP 文件夾中.
                            DownloadFileSavePath = RootPath + "\\" + localFilePath + @"\TEMP\" + myFTP.DownloadFileList[j].ClientPath.Substring(0, myFTP.DownloadFileList[j].ClientPath.IndexOf(myFTP.DownloadFileList[j].FileName));
                            if (File.Exists(DownloadFileSavePath + myFTP.DownloadFileList[j].FileName))
                            {
                                try
                                {
                                    File.Delete(DownloadFileSavePath + myFTP.DownloadFileList[j].FileName);
                                }
                                catch { }
                            }
                            NewDirectory = myFTP.DownloadFileList[j].ClientPath.Substring(0, myFTP.DownloadFileList[j].ClientPath.IndexOf(myFTP.DownloadFileList[j].FileName));
                            DirectoryList = NewDirectory.Split('\\');
                            CurDir = "";
                            for (int temp = 0; temp < DirectoryList.Length; temp++)
                            {
                                if (DirectoryList[temp] != "")
                                {
                                    CurDir = CurDir + @"\" + DirectoryList[temp];
                                    if (!Directory.Exists(RootPath + "\\" + localFilePath + @"\TEMP" + CurDir))
                                    {
                                        // 如何文件夾不存在,則建立.
                                        Directory.CreateDirectory(RootPath + "\\" + localFilePath + @"\TEMP" + CurDir);
                                    }
                                }
                            }
                            // 開始下載
                            myFTP.Download(myFTP.DownloadFileList[j].FullPath,
                                           DownloadFileSavePath,
                                           myFTP.DownloadFileList[j].FileName,
                                           myFTP.DownloadFileList[j].File_Size,
                                           myFTP.DownloadFileList[j].File_Size_Byte,
                                           j);
                            // 將下載下來的文件的修改日期和時間還原來更新文件的修改時間及時間
                            // 因為分段下載下來的文件的修改日期及時間會變為當前日期及時間.
                            FileInfo tmp = new FileInfo(DownloadFileSavePath + @"\" + myFTP.DownloadFileList[j].FileName);
                            tmp.LastWriteTime = DateTime.ParseExact(myFTP.DownloadFileList[j].Modified_DateTime, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
                            // 覆蓋原來文件
                            File.Copy(tmp.FullName, tmp.FullName.Replace("\\TEMP", ""), true);
                            try
                            {
                                File.Delete(tmp.FullName);
                            }
                            catch
                            { }
                        }
                    }
                    else
                    {
                        //文件不存在,直接下載
                        DownloadFileSavePath = RootPath + "\\" + localFilePath + @"\" + myFTP.DownloadFileList[j].ClientPath.Substring(0, myFTP.DownloadFileList[j].ClientPath.IndexOf(myFTP.DownloadFileList[j].FileName));
                        NewDirectory = myFTP.DownloadFileList[j].ClientPath.Substring(0, myFTP.DownloadFileList[j].ClientPath.IndexOf(myFTP.DownloadFileList[j].FileName));
                        DirectoryList = NewDirectory.Split('\\');
                        CurDir = "";
                        for (int temp = 0; temp < DirectoryList.Length; temp++)
                        {
                            if (DirectoryList[temp] != "")
                            {
                                CurDir = CurDir + @"\" + DirectoryList[temp];
                                if (!Directory.Exists(RootPath + "\\" + localFilePath + CurDir))
                                {
                                    //如何文件夾不存在,則建立.
                                    Directory.CreateDirectory(RootPath + "\\" + localFilePath + CurDir);
                                }
                            }
                        }
                        //開始下載
                        myFTP.Download(myFTP.DownloadFileList[j].FullPath,
                                       DownloadFileSavePath,
                                       myFTP.DownloadFileList[j].FileName,
                                       myFTP.DownloadFileList[j].File_Size,
                                       myFTP.DownloadFileList[j].File_Size_Byte,
                                       j);
                        FileInfo tmp = new FileInfo(DownloadFileSavePath + @"\" + myFTP.DownloadFileList[j].FileName);
                        tmp.LastWriteTime = DateTime.ParseExact(myFTP.DownloadFileList[j].Modified_DateTime, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);

                    }
                }

                myProgress.CloseProgress();
                try
                {
                    DeleteFolder(RootPath + "\\" + localFilePath + @"\TEMP");
                }
                catch
                { }
                #endregion

                #endregion

                return ("-1");
            }
            else
            {
                //刪除臨時文件夾
                try
                {
                    DeleteFolder(RootPath + "\\" + localFilePath + @"\TEMP");
                }
                catch
                { }
                return ("0");
            }

            #endregion
        }