Esempio n. 1
0
        public bool UploadFilesViaFTP(string newEmplFile, string updateEmplFile)
        {
            try
            {
                string host            = WebConfig.GetIVRSshServer();
                int    port            = WebConfig.GetIVRSshPort();
                string username        = WebConfig.GetIVRSshUsername();
                string password        = WebConfig.GetIVRSshPassword();
                string insertRemoteDir = WebConfig.GetIVRSshInsertRemoteDir(); // . always refers to the current directory.
                string updateRemoteDir = WebConfig.GetIVRSshUpdateRemoteDir(); // . always refers to the current directory.

                using (var sftp = new SftpClient(host, port, username, password))
                {
                    sftp.Connect();

                    #region "New Employee NCB"

                    if (File.Exists(newEmplFile))
                    {
                        // Upload file to local via SFTP
                        UploadFile(sftp, insertRemoteDir, newEmplFile);
                        Logger.Info(
                            _logMsg.Clear()
                            .SetPrefixMsg("Upload Files Via FTP")
                            .Add("FilePath", newEmplFile)
                            .ToSuccessLogString());
                    }
                    else
                    {
                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Upload Files Via FTP").Add("FilePath", newEmplFile)
                                    .Add("Error Message", "File Not Found").ToFailLogString());
                        return(false);
                    }

                    #endregion

                    #region "Update Employee NCB"

                    if (File.Exists(updateEmplFile))
                    {
                        // Upload file to local via SFTP
                        UploadFile(sftp, updateRemoteDir, updateEmplFile);
                        Logger.Info(
                            _logMsg.Clear()
                            .SetPrefixMsg("Upload Files Via FTP")
                            .Add("FilePath", updateEmplFile)
                            .ToSuccessLogString());
                    }
                    else
                    {
                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Upload Files Via FTP").Add("FilePath", updateEmplFile)
                                    .Add("Error Message", "File Not Found").ToFailLogString());
                        return(false);
                    }

                    #endregion

                    sftp.Disconnect();
                }
                return(true);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occur:\n", ex);
                Logger.Info(_logMsg.Clear().SetPrefixMsg("Upload Files Via FTP").Add("Error Message", ex.Message).ToInputLogString());
            }

            /*finally
             * {
             *  string cicPathSource = this.GetParameter(Constants.ParameterName.CICPathSource);
             *  foreach (string file in files)
             *  {
             *      string fileName = Path.GetFileName(file);
             *      StreamDataHelpers.TryToCopy(file, string.Format("{0}\\{1}", cicPathSource, fileName));
             *      StreamDataHelpers.TryToDelete(file);
             *  }
             * }*/

            return(false);
        }