Exemple #1
0
        public static bool Connect(bool OfflineMode = false, string promptedPassword = "")
        {
            string[] remoteSystem;
            bool     result = false;

            try
            {
                FTPFile = IBMiUtils.GetLocalFile("QTEMP", "FTPLOG", DateTime.Now.ToString("MMddTHHmm"), "txt");
                FtpTrace.AddListener(new TextWriterTraceListener(FTPFile));
                FtpTrace.LogUserName = false;   // hide FTP user names
                FtpTrace.LogPassword = false;   // hide FTP passwords
                FtpTrace.LogIP       = false;   // hide FTP server IP addresses

                string password = "";

                remoteSystem = CurrentSystem.GetValue("system").Split(':');

                if (promptedPassword == "")
                {
                    password = Password.Decode(CurrentSystem.GetValue("password"));
                }
                else
                {
                    password = promptedPassword;
                }

                Client = new FtpClient(remoteSystem[0], CurrentSystem.GetValue("username"), password);

                if (OfflineMode == false)
                {
                    Client.UploadDataType   = FtpDataType.ASCII;
                    Client.DownloadDataType = FtpDataType.ASCII;

                    //FTPES is configurable
                    if (IBMi.CurrentSystem.GetValue("useFTPES") == "true")
                    {
                        Client.EncryptionMode = FtpEncryptionMode.Explicit;
                    }

                    //Client.DataConnectionType = FtpDataConnectionType.AutoPassive; //THIS IS THE DEFAULT VALUE
                    Client.DataConnectionType = GetFtpDataConnectionType(CurrentSystem.GetValue("transferMode"));
                    Client.SocketKeepAlive    = true;

                    if (remoteSystem.Length == 2)
                    {
                        Client.Port = int.Parse(remoteSystem[1]);
                    }

                    Client.ConnectTimeout = 5000;
                    Client.Connect();

                    //Change the user library list on connection
                    RemoteCommand($"CHGLIBL LIBL({ CurrentSystem.GetValue("datalibl").Replace(',', ' ')}) CURLIB({ CurrentSystem.GetValue("curlib") })");

                    System.Timers.Timer timer = new System.Timers.Timer();
                    timer.Interval = 60000;
                    timer.Elapsed += new ElapsedEventHandler(KeepAliveFunc);
                    timer.Start();
                }

                result = true;
            }
            catch (Exception e)
            {
                MessageBox.Show("Unable to connect to " + CurrentSystem.GetValue("system") + " - " + e.Message, "Cannot Connect", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(result);
        }