public static bool SaveStreamToFile(string FilePath, ServerTransfer.RemoteFileInfo Infor)
 {
     try
     {
         FileStream targetStream = null;
         Stream     sourceStream = Infor.FileByteStream;
         if (File.Exists(FilePath))
         {
             File.Delete(FilePath);
         }
         using (targetStream = new FileStream(FilePath, FileMode.Create, FileAccess.Write, FileShare.None))
         {
             const int bufferLen = 65000;
             byte[]    buffer    = new byte[bufferLen];
             int       count     = 0;
             while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
             {
                 targetStream.Write(buffer, 0, count);
             }
             targetStream.Flush();
             targetStream.Close();
             sourceStream.Close();
             sourceStream.Dispose();
             sourceStream.Dispose();
         }
         return(true);
     }
     catch (Exception ex)
     {
         Logger.TranferDataLog("SaveStreamToFile Error " + ex.ToString());
         return(false);
     }
 }
 /// <summary>
 /// Chuyen so lieu
 /// </summary>
 /// <param name="FilePath"></param>
 /// <returns></returns>
 public static bool TransferFile(string FilePath)
 {
     try
     {
         System.IO.FileInfo            fileInfo          = new System.IO.FileInfo(FilePath);
         ServerTransfer.RemoteFileInfo uploadRequestInfo = new ServerTransfer.RemoteFileInfo();
         using (System.IO.FileStream stream = new System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
         {
             uploadRequestInfo.FileName       = fileInfo.Name;
             uploadRequestInfo.Length         = fileInfo.Length;
             uploadRequestInfo.FileByteStream = stream;
             uploadRequestInfo.Key            = Utility.getKey;
             ServerTransfer.Status resul = Tranfer.UploadFile(uploadRequestInfo);
             return(resul.isSucess);
         }
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
        /// <summary>
        /// DowLoad file từ Server Ve Client
        /// </summary>
        public static void GetScriptAndFlusToDataBase()
        {
            // Logger.TranferDataLog(isGetScriptProcess.ToString() + "Runing time " + DateTime.Now.ToString());
            if (isGetScriptProcess)
            {
                return;
            }
            try
            {
                isGetScriptProcess = true;
                int TopRecord = clsFormat.IntConvert(Utility.GetConfigValue(SettingsKey.TOPRECORD));
                Logger.TranferDataLog("--------------------------------------------------------");

                ServerTransfer.DownloadRequest requestData = new ServerTransfer.DownloadRequest();
                ServerTransfer.RemoteFileInfo  fileInfo    = new ServerTransfer.RemoteFileInfo();
                requestData.TopRecord = TopRecord;
                requestData.Key       = Utility.getKey;
                requestData.IPAddress = vnyi.Utility.RES.clsMisc.GetIP();
                requestData.Type      = "script";
                fileInfo = Tranfer.DownloadFile(requestData);

                if (fileInfo.isSuccess)
                {
                    string Directory = Utility.SaveFilePath;
                    if (!File.Exists(Directory))
                    {
                        System.IO.Directory.CreateDirectory(Directory);
                    }
                    string FilePath = System.IO.Path.Combine(Directory, fileInfo.FileName);
                    if (File.Exists(FilePath))
                    {
                        File.Delete(FilePath);
                    }
                    if (SaveStreamToFile(FilePath, fileInfo))
                    {
                        bool isInsertLogFile = false;
                        do
                        {
                            isInsertLogFile = vnyi.Library.PUB.TranferData.transferInsertQueue(new Guid(Utility.getKey), FilePath, 2);
                        }while (!isInsertLogFile);
                        ServerTransfer.MessegerResquest request = new ServerTransfer.MessegerResquest
                        {
                            FileName = fileInfo.FileName,
                            FilePath = fileInfo.FilePath,
                            Status   = Meta.log.Status.Success,
                            Title    = "Get File Success " + fileInfo.FileName,
                            Type     = 2,
                            Key      = Utility.getKey
                        };
                        if (fileInfo.RowVersion > 0)
                        {
                            request.RowVersionLog = fileInfo.RowVersion;
                        }
                        Logger.TranferDataLog("RowVersion" + fileInfo.RowVersion.ToString());
                        ServerTransfer.Status st = null;
                        do
                        {
                            st = Tranfer.LogerMesseger(request);
                        }while (!st.isSucess);
                        if (fileInfo.TotalRecord > TopRecord)
                        {
                            GetScriptAndFlusToDataBase();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.TranferDataLog("Get Script Error " + ex.ToString());
            }
            finally
            {
                isGetScriptProcess = false;
            }
        }