//获取图片线程操作 private void getImageThread() { //初始化 sftp.Connect(); //与服务器建立连接 MemoryStream ms; //内存区域的流,用来读取截图 MyInvoke mi = new MyInvoke(UpdateForm); //窗体控件的线程间安全调用 arr = sftp.GetFileList("/root/img", filename); //获取文件列表 progressbar = 10; //判断如果没有相应的截图时,则报错 if (arr.Count == 0) { MessageBox.Show("没有该日期的数据!"); progressbar = 0; error = true; return; } //先读取出第一个截图 ms = new MemoryStream(sftp.Get("/root/img/" + arr[0])); images = new Image[arr.Count]; images[0] = Image.FromStream(ms); pictureBox1.Image = images[0]; flag = 0; ms.Close(); progressbar = 12; //如果有更多图片,则一一读取 if (arr.Count > 1) { for (int i = 1; i < arr.Count; i++) { Console.WriteLine(arr[i]); ms = new MemoryStream(sftp.Get("/root/img/" + arr[i])); images[i] = Image.FromStream(ms); ms.Close(); if (progressbar >= 92) { progressbar = 92; } else { progressbar = progressbar + 4; } } } sftp.Disconnect(); //断开服务器连接 this.BeginInvoke(mi); //调用控件的安全线程 progressbar = 100; }
/// <summary> /// Function Name : SftpDisconnect() /// Description : This method is used to close the sftp connection that have been used for data transfer. /// </summary> public void SftpDisconnect() { try { Thread.Sleep(2000); _objSftpClient.Disconnect(); } catch (Exception ex) { Console.WriteLine("Error while process try to disconnect with SFTP.Error: " + ex.Message); throw ex; } }
//监控线程thread是否完成 private void timer1_Tick(object sender, EventArgs e) { if (progressbar == 100 || error == true) { sftp.Disconnect(); if (thread.ThreadState == ThreadState.Running) { thread.Abort(); } if (error == true) { //使按钮不可用 button2.Enabled = false; button3.Enabled = false; } //启用确定按钮 button1.Enabled = true; } }
public bool GetFiles() { bool status = false; _sftp = new SFTP(m_sftpUserName, m_sftpKey); _sftp.EnableLogging(); _sftp.HostAddress = m_sftpSite; _sftp.UserName = m_sftpSiteUserName; _sftp.SshKeyFile = m_identityFile; _sftp.SshPassphrase = @"t0y0ta69"; try { try { // connect _sftp.Connect(); } catch (KellermanSoftware.NetSFtpLibrary.Implementation.SftpException ke) { LogMsg("NEXCEPTION:SftpMgr::_sftp.Connect():ECaught:" + ke.Message); } // get a directory list List<FTPFileInfo> rFiles = _sftp.GetAllFiles(m_sftpSiteRemoteFolder); // process the list foreach (FTPFileInfo file in rFiles) { if (file.Equals(".") || file.Equals("..")) continue; string f = this.ParseFileName(file.FileName); // if we got it, don't get it if ( this.CheckDb(f) ) continue; try { // get the file and put in the watch folder to be processed _sftp.DownloadFile(m_moveToProcessFolder + f, file.FileName); string newPlace = m_moveAfterProcessingFolder + f; bool moved =_sftp.MoveFile(file.FileName, newPlace ); // update the database to indicate file has been downloaded this.UpdateDb(f); } catch (SystemException se) { LogMsg("NEXCEPTION:SftpMgr::GetFile():ECaught::"+se.Message + se.StackTrace ); } } _sftp.Disconnect(); _sftp.Dispose(); status = true; } catch (SystemException se) { LogMsg("NEXCEPTION:SftpMgr::GetFile():ECaught:TryingToGetFile::" + se.Message); } return status; }
public static void TestFTPAccount(FTPAccount account, bool silent) { string msg = string.Empty; string sfp = account.GetSubFolderPath(); switch (account.Protocol) { case FTPProtocol.SFTP: SFTP sftp = new SFTP(account); if (!sftp.IsInstantiated) { msg = "An SFTP client couldn't be instantiated, not enough information.\nCould be a missing key file."; } else if (sftp.Connect()) { List <string> createddirs = new List <string>(); if (!sftp.DirectoryExists(sfp)) { createddirs = sftp.CreateMultipleDirectorys(FTPHelpers.GetPaths(sfp)); } if (sftp.IsConnected) { msg = (createddirs.Count == 0) ? "Connected!" : "Conected!\nCreated folders;\n"; for (int x = 0; x <= createddirs.Count - 1; x++) { msg += createddirs[x] + "\n"; } msg += " \n\nPing results:\n " + SendPing(account.Host, 3); sftp.Disconnect(); } } break; default: using (FTP ftpClient = new FTP(account)) { try { //DateTime time = DateTime.Now; ftpClient.Test(sfp); msg = "Success!"; } catch (Exception e) { if (e.Message.StartsWith("Could not change working directory to")) { try { ftpClient.MakeMultiDirectory(sfp); ftpClient.Test(sfp); msg = "Success!\nAuto created folders: " + sfp; } catch (Exception e2) { msg = e2.Message; } } else { msg = e.Message; } } } if (!string.IsNullOrEmpty(msg)) { string ping = SendPing(account.Host, 3); if (!string.IsNullOrEmpty(ping)) { msg += "\n\nPing results:\n" + ping; } if (silent) { // Engine.MyLogger.WriteLine(string.Format("Tested {0} sub-folder path in {1}", sfp, account.ToString())); } else { //MessageBox.Show(msg, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); } } break; } if (silent) { DebugHelper.WriteLine(string.Format("Tested {0} sub-folder path in {1}", sfp, account.ToString())); } else { MessageBox.Show(msg, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); } }
public bool GetFiles() { bool status = false; _sftp = new SFTP(m_sftpUserName, m_sftpKey); _sftp.EnableLogging(); _sftp.HostAddress = m_sftpSite; _sftp.UserName = m_sftpSiteUserName; _sftp.SshKeyFile = m_identityFile; _sftp.SshPassphrase = @"t0y0ta69"; try { try { // connect _sftp.Connect(); } catch (KellermanSoftware.NetSFtpLibrary.Implementation.SftpException ke) { LogMsg("NEXCEPTION:AMSSftpMgr::_sftp.Connect():ECaught:" + ke.Message); return(status); } // get a directory list List <FTPFileInfo> rFiles = _sftp.GetAllFiles(m_sftpSiteRemoteFolder); // process the list foreach (FTPFileInfo file in rFiles) { if (file.Equals(".") || file.Equals("..")) { continue; } string f = this.ParseFileName(file.FileName); // if we got it, don't get it if (this.CheckDb(f)) { continue; } try { // get the file and put in the watch folder to be processed _sftp.DownloadFile(m_moveToProcessFolder + f, file.FileName); // if we're crunching it then do it if (this.m_moveToCrunchFlag) { _sftp.DownloadFile(m_moveToCrunchFolder + f, file.FileName); } string newPlace = m_moveAfterProcessingFolder + f; bool moved = _sftp.MoveFile(file.FileName, newPlace); bool dirDone = _sftp.CreateDirectory("tested"); // update the database to indicate file has been downloaded this.UpdateDb(f); } catch (SystemException se) { LogMsg("NEXCEPTION:AMSSftpMgr::GetFile():ECaught::" + se.Message + se.StackTrace); } } _sftp.Disconnect(); _sftp.Dispose(); status = true; } catch (SystemException se) { LogMsg("NEXCEPTION:AMSSftpMgr::GetFile():ECaught:TryingToGetFile::" + se.Message); } return(status); } // getFile()