/// <summary> /// Try to connect to the account found in Common.Profile, then raise /// TryConnectCompleted to show if the attempt was successful /// </summary> public static void TryConnect() { try { Connect(); // Setup the event handler for the async upload if (Common.Profile.Protocol != FtpProtocol.SFTP) { ftpc.PutFileAsyncCompleted += CaptureControl.ImageUploaded; } // If the image has been captured, start the upload if (Common.IsImageCaptured) { CaptureControl.DoStartUpload(); TryConnectCompleted(null, new TryConnectEventArgs { Success = true }); } } catch { // If connecting failed, raise TryConnectCompleted with Success set to false TryConnectCompleted(null, new TryConnectEventArgs { Success = false }); } }
/// <summary> /// Upload the captured image, file path found in CapturedImage.LocalPath /// </summary> public static void UploadCapturedImage() { if (FTP) { ftpc.PutFileAsync(CapturedImage.LocalPath, CapturedImage.RemotePath, FileAction.CreateNew); } else { EventWaitHandle wait = new AutoResetEvent(false); using (var file = File.OpenRead(CapturedImage.LocalPath)) { var asynch = sftpc.BeginUploadFile(file, CapturedImage.RemotePath, delegate { Console.Write("\nCallback called."); wait.Set(); }, null); var sftpASynch = asynch as Renci.SshNet.Sftp.SftpUploadAsyncResult; while (!sftpASynch.IsCompleted) { if (sftpASynch.UploadedBytes > 0) { Console.Write("\rUploaded {0} bytes ", sftpASynch.UploadedBytes); } Thread.Sleep(100); } Console.Write("\rUploaded {0} bytes!", sftpASynch.UploadedBytes); sftpc.EndUploadFile(asynch); wait.WaitOne(); } CaptureControl.ImageUploaded(); } }