Beispiel #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            using (WinSCP.Session session = new WinSCP.Session())
            {
                fingerprint = session.ScanFingerprint(sessionOptions, "MD5");
                sessionOptions.SshHostKeyFingerprint = fingerprint;
                session.Open(sessionOptions);
                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;
                string sourcePath      = @"c:\test\setup-x86_64.exe";
                string destinationPath = @"/c:/MalCode/";
                string textBxInput     = this.txtBxFilename.Text;
                if (!string.IsNullOrEmpty(textBxInput))
                {
                    sourcePath = @"c:\test\" + textBxInput;
                }


                TransferOperationResult transferResult;
                transferResult = session.PutFiles(sourcePath, destinationPath, false, transferOptions);

                transferResult.Check();

                foreach (TransferEventArgs transfer in transferResult.Transfers)
                {
                    System.Console.WriteLine("Upload of {0} succeeded ", transfer.FileName);
                }
            }
        }
Beispiel #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Sftp,
                HostName = "sftp.maitsa.com",
                UserName = "******",
                SshHostKeyFingerprint = null,
                //RUTA DEL ARCHIVO DE LA KEY PARA CONEXION AL SFTOP
                SshPrivateKeyPath = @"C:\NOVOTIC\CLIENTES\IDRTEXT\SFTP MAITSA\private-key.ppk",
            };

            using (Session session = new Session())
            {
                session.ScanFingerprint(sessionOptions, "SHA-256");

                sessionOptions.SshHostKeyFingerprint = session.ScanFingerprint(sessionOptions, "SHA-256");
                Console.WriteLine(session.ScanFingerprint(sessionOptions, "SHA-256"));

                session.Open(sessionOptions);



                string xmlLogPath = session.XmlLogPath;
                object p          = xmlLogPath;

                Console.WriteLine(p.ToString());

                TransferOptions oTransferOptions = new TransferOptions();
                oTransferOptions.TransferMode = WinSCP.TransferMode.Binary;

                TransferOperationResult transferResult;

                transferResult = session.GetFiles("/sftp-maitsa/customs-broker-integration/api/prod/idrtext/processed/PO626/*", @"C:\NOVOTIC\CLIENTES\IDRTEXT\SFTP MAITSA\", false, oTransferOptions);

                transferResult.Check();

                foreach (TransferEventArgs transfer in transferResult.Transfers)
                {
                    Console.WriteLine("Download of {0} succeeded", transfer.FileName);
                }
            }
        }
Beispiel #3
0
        private void btnDownload_Click(object sender, EventArgs e)
        {
            using (WinSCP.Session session = new WinSCP.Session())
            {
                fingerprint = session.ScanFingerprint(sessionOptions, "MD5");
                sessionOptions.SshHostKeyFingerprint = fingerprint;
                session.Open(sessionOptions);
                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;

                TransferOperationResult transferResult;
                transferResult = session.GetFiles(@"/c:/MalCode/HiWinForm.exe", @"c:\test\", false, transferOptions);

                transferResult.Check();

                foreach (TransferEventArgs transfer in transferResult.Transfers)
                {
                    System.Console.WriteLine("Upload of {0} succeeded ", transfer.FileName);
                }
            }
        }
Beispiel #4
0
        private void button1_Click_3(object sender, EventArgs e)
        {
            using (WinSCP.Session session = new WinSCP.Session())
            {
                fingerprint = session.ScanFingerprint(sessionOptions, "MD5");
                sessionOptions.SshHostKeyFingerprint = fingerprint;
                session.Open(sessionOptions);
                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;

                TransferOperationResult transferResult;
                session.RemoveFiles(@"/c:/MalCode/WinForm.exe");

                //transferResult.Check();

                //foreach (TransferEventArgs transfer in transferResult.Transfers)
                //{
                //    System.Console.WriteLine("Upload of {0} succeeded ", transfer.FileName);
                //}
            }
        }
Beispiel #5
0
        private void btnFileExists_Click(object sender, EventArgs e)
        {
            using (WinSCP.Session session = new WinSCP.Session())
            {
                fingerprint = session.ScanFingerprint(sessionOptions, "MD5");
                sessionOptions.SshHostKeyFingerprint = fingerprint;
                session.Open(sessionOptions);

                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;

                TransferOperationResult transferResult;
                if (session.FileExists(@"/c:/sbmon_complete"))
                {
                    MessageBox.Show("File {0} exists", @"/c:/sbmon_complete");
                }
                else
                {
                    MessageBox.Show("File does not exist");
                }
            }
        }
        public string RunFtp(string hostIP, string port, string user, string pass, string remoteDirectory, string remoteFilePattern, string localFileDirectory, string pharmacyName, string localFileName, int numHoursGoback, string ram, ref DataTable tblExcel)
        {
            string result = "";
            string localFileDirWithDateTime;
            string filePrefix = "SFTPDownload";

            try
            {
                localFileDirWithDateTime = Helper.CreateDirectory(localFileDirectory, DateTime.Now, pharmacyName);
            }
            catch (Exception ex)
            {
                LogObj.WriteExcelrow("Failure!", enMsgType.enMsgType_Info, filePrefix,
                                     pharmacyName, "", DateTime.Now, "Failed trying to create a local Dir in " + localFileDirectory + ". Ftp for this pharm stopped.", ref tblExcel);
                return("Failure trying to create a directory in " + localFileDirectory + " for pharmacy " + pharmacyName + ". error:" + ex.Message);
            }

            try
            {//------------------------------------------------------------------------------------------------
                WinSCP.SessionOptions sessionOptions = new WinSCP.SessionOptions
                {
                    Protocol   = WinSCP.Protocol.Sftp,
                    HostName   = hostIP,
                    UserName   = user,
                    Password   = pass,
                    PortNumber = Convert.ToInt32(port)

                                 /* use these to connect to FTP, and leave out the fingerPrint.:
                                  *
                                  * _with1.Protocol = Protocol.Ftp;
                                  * _with1.FtpSecure = FtpSecure.Explicit;
                                  *
                                  */
                };
                string fingerprint = null;
                using (WinSCP.Session session = new WinSCP.Session())
                {
                    fingerprint = session.ScanFingerprint(sessionOptions);
                }
                if (fingerprint == null)
                {
                    throw new Exception("Couldnt determine Host Fingerprint");
                }
                else
                {
                    sessionOptions.SshHostKeyFingerprint = fingerprint;
                }


                using (WinSCP.Session session = new WinSCP.Session())
                {
                    sessionOptions.Timeout = TimeSpan.FromMinutes(6);  // 6 min timeout
                    //session.ExecutablePath = @"C:\Software\v11\winscp.exe";
                    session.Open(sessionOptions);

                    WinSCP.TransferOptions transferOptions = new WinSCP.TransferOptions();
                    transferOptions.TransferMode = WinSCP.TransferMode.Binary;

                    WinSCP.TransferOperationResult transferResult = default(WinSCP.TransferOperationResult);
                    transferResult = session.GetFiles(remoteDirectory + remoteFilePattern, localFileDirectory, false, transferOptions);

                    transferResult.Check();
                    //THrows the first error if not successful
                    foreach (WinSCP.TransferEventArgs transfer in transferResult.Transfers)
                    {
                        string downfileName = transfer.FileName;

                        DateTime remoteWriteTime =
                            session.GetFileInfo(downfileName).LastWriteTime;               //get the DateTime of the file.

                        if (remoteWriteTime >= DateTime.Now.AddHours(numHoursGoback * -1)) // ignore if older that the num Hours Goback spec by user
                        {
                            string localFilename = localFileDirWithDateTime + downfileName;
                            //try
                            //{

                            result += downfileName + "\r\n";
                            LogObj.WriteExcelrow("Success", enMsgType.enMsgType_Info, filePrefix,
                                                 pharmacyName, downfileName, remoteWriteTime, "", ref tblExcel);
                            // }
                            //catch (Exception ex)
                            //{
                            //    LogObj.WriteExcelrow("Failure!", enMsgType.enMsgType_Info, filePrefix,
                            //                                    pharmacyName, downfileName, remoteWriteTime, ex.Message, ref tblExcel);

                            //    return "Failure occurred trying to Download SecureFtpWinSCP file " + downfileName + " for HostIp " + hostIP + " directory " + remoteDirectory + " files " + remoteFilePattern + " error:" + ex.Message + "\r\n";
                            //}
                        }
                        else
                        {
                            result += "Should be Ignored" + downfileName + " but still nowloaded." + "\r\n";

                            LogObj.WriteExcelrow("Success", enMsgType.enMsgType_Info, filePrefix,
                                                 pharmacyName, downfileName, remoteWriteTime, " but still nowloaded.", ref tblExcel);

                            //LogObj.WriteExcelrow("Ignored", enMsgType.enMsgType_Info, filePrefix,
                            //                                    pharmacyName, downfileName, file.Attributes.LastWriteTime, "its too old", ref tblExcel);
                        }
                    }
                }
                //-------------------------------------------------------------------------------------------------------------------


                return("Successfully downloaded to local dir " + localFileDirectory + " the following files: " + result);
            }
            catch (WinSCP.SessionRemoteException ex)
            {
                LogObj.WriteExcelrow("Failure!", enMsgType.enMsgType_Info, filePrefix, pharmacyName, "", DateTime.Now, "Check the remote settings. " + ex.Message, ref tblExcel);
                return("Failure occurred trying to connect to download SFtp for HostIp " + hostIP + " directory " + remoteDirectory + remoteFilePattern + " error:" + ex.Message + "\r\n");
            }

            catch (System.TimeoutException ex)
            {
                LogObj.WriteExcelrow("Failure!", enMsgType.enMsgType_Info, filePrefix, pharmacyName, "", DateTime.Now, ex.Message, ref tblExcel);
                return("Failure occurred trying to connect to download SFtp for HostIp " + hostIP + " directory " + remoteDirectory + remoteFilePattern + " error:" + ex.Message + "\r\n");
            }


            catch (Exception ex)
            {
                LogObj.WriteExcelrow("Failure!", enMsgType.enMsgType_Info, filePrefix, pharmacyName, "", DateTime.Now, ex.Message, ref tblExcel);

                return("Failure occurred trying to connect to download SFtp for HostIp " + hostIP + " directory " + remoteDirectory + remoteFilePattern + " error:" + ex.Message + "\r\n");
            }
        }